Jo’s BookshelfCocoa – NSView backgroundcolor

学习过iOS开发的同学对于如何设置一个控件的backgroundColor是在熟悉不过的了, 但是相对于macOS开发中, NSView的属性中并没有发现color相关的属性, 那么如何设置呢?

  • macOS中, 将 NSViewbackgroundColor 放到了 layer 中:
@IBOutlet var dragcontentView: NSView!

self.dragcontentView.layer?.backgroundColor = NSColor.clear.cgColor;

B&R后发现并没有改变颜色, 因为我们未设置时候允许NSViewwantsLayer属性:

@IBOutlet var dragcontentView: NSView!

self.dragcontentView.wantsLayer = true;

self.dragcontentView.layer?.backgroundColor = NSColor.clear.cgColor;

搞定!