最好的方法来更改NSView的背景颜色
我正在寻找更改NSView的backgroundColor的最佳方法。 我也想能够为NSView设置适当的alpha蒙版。 就像是: 
 myView.backgroundColor = [NSColor colorWithCalibratedRed:0.227f green:0.251f blue:0.337 alpha:0.8]; 
 我注意到NSWindow有这个方法,我不是NSColorWheel或NSImage后台选项的大粉丝,但是如果他们是最好的,愿意使用。 
是的,你自己的回答是对的。 你也可以使用cocoa方法:
 - (void)drawRect:(NSRect)dirtyRect { // set any NSColor for filling, say white: [[NSColor whiteColor] setFill]; NSRectFill(dirtyRect); [super drawRect:dirtyRect]; } 
 一个简单而有效的解决scheme是将视图configuration为使用Core Animation层作为其后备存储。 然后你可以使用-[CALayer setBackgroundColor:]设置图层的背景颜色。 
 - (void)awakeFromNib { self.wantsLayer = YES; // NSView will create a CALayer automatically } - (BOOL)wantsUpdateLayer { return YES; // Tells NSView to call `updateLayer` instead of `drawRect:` } - (void)updateLayer { self.layer.backgroundColor = [NSColor colorWithCalibratedRed:0.227f green:0.251f blue:0.337 alpha:0.8].CGColor; } 
而已!
如果你是一个故事情人,这是一种你不需要任何代码的方式 。
 将NSBox作为子视图添加到NSView,并将NSBox的框架调整为与NSView一样。 
在Storyboard或XIB中,将标题位置更改为无,将框types更改为自定义 ,将边框types更改为“无”,并将边框颜色更改为任何您喜欢的颜色。
这是一个截图:
  
 
这是结果:
  
 
想想我想出了如何做到这一点:
 - (void)drawRect:(NSRect)dirtyRect { // Fill in background Color CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort]; CGContextSetRGBFillColor(context, 0.227,0.251,0.337,0.8); CGContextFillRect(context, NSRectToCGRect(dirtyRect)); } 
如果先将WantsLayer设置为YES,则可以直接操作图层背景。
 [self.view setWantsLayer:YES]; [self.view.layer setBackgroundColor:[[NSColor whiteColor] CGColor]]; 
我经历了所有这些答案,不幸的是他们都为我工作。 不过,经过大约一个小时的search,我发现这个非常简单的方法:)
 myView.layer.backgroundColor = CGColorCreateGenericRGB(0, 0, 0, 0.9); 
编辑/更新: Xcode 8.3.1•Swift 3.1
 extension NSView { var backgroundColor: NSColor? { get { guard let color = layer?.backgroundColor else { return nil } return NSColor(cgColor: color) } set { wantsLayer = true layer?.backgroundColor = newValue?.cgColor } } } 
用法:
 let myView = NSView(frame: NSRect(x: 0, y: 0, width: 100, height: 100)) print(myView.backgroundColor ?? "none") // NSView's background hasn't been set yet = nil myView.backgroundColor = .red // set NSView's background color to red color print(myView.backgroundColor ?? "none") view.addSubview(myView) 
最佳解决scheme
 - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { self.wantsLayer = YES; } return self; } - (void)awakeFromNib { float r = (rand() % 255) / 255.0f; float g = (rand() % 255) / 255.0f; float b = (rand() % 255) / 255.0f; if(self.layer) { CGColorRef color = CGColorCreateGenericRGB(r, g, b, 1.0f); self.layer.backgroundColor = color; CGColorRelease(color); } } 
在Swift中:
 override func drawRect(dirtyRect: NSRect) { NSColor.greenColor().setFill() NSRectFill(dirtyRect) super.drawRect(dirtyRect) } 
我testing了以下,它为我工作(在斯威夫特):
 view.wantsLayer = true view.layer?.backgroundColor = NSColor.blackColor().colorWithAlphaComponent(0.5).CGColor 
在Swift 3中,你可以创build一个扩展来做到这一点:
 extension NSView { func setBackgroundColor(_ color: NSColor) { wantsLayer = true layer?.backgroundColor = color.cgColor } } // how to use btn.setBackgroundColor(NSColor.gray) 
使用NSBox,这是NSView的子类,使我们可以轻松地风格
Swift 3
 let box = NSBox() box.boxType = .custom box.fillColor = NSColor.red box.cornerRadius = 5 
看看RMSkinnedView 。 你可以在Interface Builder中设置NSView的背景颜色。
在swift中,你可以inheritanceNSView并做到这一点
 class MyView:NSView { required init?(coder: NSCoder) { super.init(coder: coder); self.wantsLayer = true; self.layer?.backgroundColor = NSColor.redColor().CGColor; } }