I set background image for the main window of my app inside application delegate:
func applicationDidFinishLaunching(_ aNotification: Notification) {
if let mw = NSApplication.shared.mainWindow {
mw.isMovableByWindowBackground = true
mw.backgroundColor = NSColor.init(red: 0.3, green: 0.3, blue: 0.3, alpha: 1.0)
if let cv = mw.contentView {
cv.wantsLayer = true
let image = NSImage(named: NSImage.Name(rawValue: "bkg"))
cv.layer!.contents = image
}
else {
print("Content view is not initialized yet")
}
}
else {
print("Main window is not initialized yet")
}
}
However, occasionally I see that mainWindow
returning nil
. So I guess applicationDidFinishLaunching
is not the best place to put my code. Also sometimes I notice during app lunch default background appears in the blink of an eye and then background image is applied.
Where is the best place to put my code for applying background image?