I am trying to create an NSWindow and make the window float above full screen apps. I have tried this using two different approaches which are outlined below.
Method 1 - storyboard (working)
I have a project with a storyboard that includes a window controller. This window controller has the following class assigned to it in the identity inspector.
class WindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
guard let window = self.window else { return }
// Enable full screen
window.backgroundColor = .red
window.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.mainMenuWindow)))
window.styleMask = [.fullScreen]
window.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
}
}
This method works without any issues as seen in this screenshot. However, I would prefer to create the NSWindow programmatically.
Method 2 - programmatically (not working)
In this approach I have the following WindowController class:
class WindowController: NSWindowController {
init() {
super.init(window: NSWindow())
guard let window = self.window else { return }
setup(window: window)
}
func setup(window: NSWindow) {
window.backgroundColor = .red
window.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.mainMenuWindow)))
window.styleMask = [.fullScreen]
window.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Along with the following in my appDelegate:
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let window = WindowController()
func applicationDidFinishLaunching(_ aNotification: Notification) {
window.showWindow(self)
}
}
Despite this successfully creating a NSWindow that floats above windowed apps, as seen here, the NSWindow refuses to float above full screen screen apps.
From the time I have spent trying to debug the issue it seems as though window.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
does not do anything when creating the NSWindow programmatically in the second approach.
I am fairly new to swift so any help regarding why the NSWindow is not floating above fullscreen apps in the second approach would be greatly appreciated!
Versions:
Swift 5
Xcode 11.2.1