【发布时间】:2011-11-24 08:47:21
【问题描述】:
我想设置一个带有自定义 NSColor 背景 ([NSColor colorWithPatternImage:[NSImage imageNamed:@"pattern.png"]]) 的自定义视图(不是主视图)。我试过制作一个自定义视图类:
.h
#import <AppKit/AppKit.h>
@interface CustomBackground : NSView {
NSColor *background;
}
@property(retain) NSColor *background;
@end
.m
#import "CustomBackground.h"
@implementation CustomBackground
@synthesize background;
- (void)drawRect:(NSRect)rect
{
[background set];
NSRectFill([self bounds]);
}
- (void)changeColor:(NSColor*) aColor
{
background = aColor;
[aColor retain];
}
@end
然后在 AppDelegate 中:
[self.homeView changeColor:[NSColor colorWithPatternImage:[NSImage imageNamed:@"pattern.png"]]];
但什么也没发生,颜色保持不变。怎么了?或者有没有更简单的方法? NSView 没有backgroundColor 属性:(
【问题讨论】:
-
这应该是最好的办法:stackoverflow.com/questions/2962790/…
标签: objective-c macos cocoa