我想用自定義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