简体   繁体   中英

Why does my NSWindow not float above full screen apps when created programmatically? (Swift 5)

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

7 months later and I managed to get it working by changing the window's styleMask to borderless

window.styleMask = [.borderless]

Better late then never I suppose...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM