El Tomato El Tomato - 2 months ago 26
Swift Question

Updating Custom NSWindow Appearance

enter image description here

As shown above, I have a view controller (

NSViewController
) and a window controller (
NSWindowController
). And I have a custom window (
NSWindow
) so that I can customize the look of the main window. Specially, I just want to set a particular color to its background color.

class BasicBorderlessWindow: NSWindow {
override init(contentRect: NSRect, styleMask style: NSWindowStyleMask, backing bufferingType: NSBackingStoreType, defer flag: Bool) {
super.init(contentRect: contentRect, styleMask: style, backing: .buffered, defer: false)
self.isOpaque = false
self.backgroundColor = NSColor.green
}
}


Now, the entire window is green.

What I want to know is a way of changing the color of entire window once the application starts up. The subclassed window (BasicBorderlessWindow) is wired to the window controller. So I can access it from the view controller. I can create a function to set a new color so that I can call it from the view controller. But I cannot refresh the window. So how do I change window's background color and update it? Thanks.

Answer Source

Try setting the window's background color from within the window controller.

self.window?.backgroundColor = NSColor.blue