Skip to content

Instantly share code, notes, and snippets.

@revblaze
Created December 8, 2020 18:38
Show Gist options
  • Save revblaze/0b572ce78999916028f423866ebb1dc5 to your computer and use it in GitHub Desktop.
Save revblaze/0b572ce78999916028f423866ebb1dc5 to your computer and use it in GitHub Desktop.
Extension: Easily set NSView background color
extension NSView {
var backgroundColor: NSColor? {
get {
if let colorRef = self.layer?.backgroundColor {
return NSColor(cgColor: colorRef)
} else {
return nil
}
}
set {
self.wantsLayer = true
self.layer?.backgroundColor = newValue?.cgColor
}
}
}
@revblaze
Copy link
Author

revblaze commented Dec 8, 2020

Usage Example

override func viewDidLoad() {
  super.viewDidLoad()
  view.backgroundColor = .clear   // .white ? .black
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment