NSView、设置BackgroundColor
NSView *view = [[NSView alloc] initWithFrame:NSMakeRect(100, 150, 100, 50)];
view.wantsLayer = YES;(很重要,不设置是否显示layer,默认是不显示,设置颜色也不会显示)
view.layer.backgroundColor = [NSColor blueColor].CGColor;(设置背景色要通过layer来设置)
[view setNeedsDisplay:YES];(可要也可不要)
[self.view addSubview:view];
NSButton 设置 BackgroundColor
方法一:
[[button cell] setBackgroundColor:[NSColor redColor]];
方法二:
+(NSImage *)swatchWithColor:(NSColor *)color size:(NSSize)size
{
NSImage *image = [[NSImage alloc] initWithSize:size];
[image lockFocus];
[color drawSwatchInRect:NSMakeRect(0, 0, size.width, size.height)];
[image unlockFocus];
return image;
}
NSImage *image = [NSImage swatchWithColor:[NSColor greenColor] size:button.frame.size]; [[button cell] setBackgroundColor:[NSColor redColor]];
button.image = image;