我想用自定义NSColor背景([NSColor colorWithPatternImage:[NSImage imageNamed:@"pattern.png"]]
)设置自定义视图(不是主视图)。我试着做一个自定义的视图类:设置NSView的背景颜色
.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
财产:(
这应该是最好的方法:http://stackoverflow.com/questions/2962790/best-way-to-change-the-background-color-for-an-nsview – roman