How can I enter FullScreen mode in Cocoa on startup?

I'm using Swift for an OS X app and I want it to enter Full Screen on startup. I've entered the following code in my appdelegate.swift, but it's giving me an error.


    func window(window: NSWindow, willUseFullScreenPresentationOptions proposedOptions: NSApplicationPresentationOptions) -> NSApplicationPresentationOptions {
        return NSApplicationPresentationOptions.AutoHideToolbar |
            NSApplicationPresentationOptions.AutoHideMenuBar |
            NSApplicationPresentationOptions.FullScreen |
            proposedOptions
    }


the error is: "Binary operator '|' cannot be applied to two 'NSApplicationPresentationOptions' operands. I've tried to find a tutorial to try and do this, but I can't seem to find anything that explains how to enter fullscreen on startup. I'm still pretty knew to Swift and OS X programming so please forgive me if I've made a huge newbie error.


Any help would be greatly appreciated.


Thank you so much.

Accepted Reply

Hi QuincyMorris,


Thanks for the reply. It didn't help me with the full screen problem, but it helped to understand the issues at hand and then find the solution I wanted. Just for those who might come to this looking to open their app in Full Screen mode, this is what I used in my main ViewController:


override func viewDidAppear() {
        let presOptions: NSApplicationPresentationOptions = ([.FullScreen,.AutoHideMenuBar])
/*These are all of the options for NSApplicationPresentationOptions 
            .Default                 
            .AutoHideDock              |   /
            .AutoHideMenuBar           |   /
            .DisableForceQuit          |   /
            .DisableMenuBarTransparency|   /
            .FullScreen                |   /
            .HideDock                  |   /
            .HideMenuBar               |   /
            .DisableAppleMenu          |   /
            .DisableProcessSwitching   |   /
            .DisableSessionTermination |   /
            .DisableHideApplication    |   /
            .AutoHideToolbar
            .HideMenuBar               |   /
            .DisableAppleMenu          |   /
            .DisableProcessSwitching   |   /
            .DisableSessionTermination |   /
            .DisableHideApplication    |   /
            .AutoHideToolbar */
     
        let optionsDictionary = [NSFullScreenModeApplicationPresentationOptions :
            NSNumber(unsignedLong: presOptions.rawValue)]
     
        self.view.enterFullScreenMode(NSScreen.mainScreen()!, withOptions:optionsDictionary)
        self.view.wantsLayer = true
    }


I was able to find this source code from the following link:

http://blogs.wcode.org/2015/06/howto-create-a-locked-down-fullscreen-cocoa-application-and-implement-nslayoutconstraints-using-swift/


Again, thanks for the help and the point in the right direction and I hope that this can help others.

Replies

In the current version of Swift, things like NSApplicationPresentationOptions are option sets. The easiest way to create one from literal options is like this:


[NSApplicationPresentationOptions.AutoHideToolbar, NSApplicationPresentationOptions.AutoHideMenuBar, NSApplicationPresentationOptions.FullScreen]


or in a context where the type can be inferred:


[.AutoHideToolbar, .AutoHideMenuBar, .FullScreen]


so you should be able to write your return statement like this:


return proposedOptions.union ([.AutoHideToolbar, .AutoHideMenuBar, .FullScreen])


Recent versions of Swift have started avoiding "near enough" operator recyclings ( | ), and using actual words for the function (union) instead.

Hi QuincyMorris,


Thanks for the reply. It didn't help me with the full screen problem, but it helped to understand the issues at hand and then find the solution I wanted. Just for those who might come to this looking to open their app in Full Screen mode, this is what I used in my main ViewController:


override func viewDidAppear() {
        let presOptions: NSApplicationPresentationOptions = ([.FullScreen,.AutoHideMenuBar])
/*These are all of the options for NSApplicationPresentationOptions 
            .Default                 
            .AutoHideDock              |   /
            .AutoHideMenuBar           |   /
            .DisableForceQuit          |   /
            .DisableMenuBarTransparency|   /
            .FullScreen                |   /
            .HideDock                  |   /
            .HideMenuBar               |   /
            .DisableAppleMenu          |   /
            .DisableProcessSwitching   |   /
            .DisableSessionTermination |   /
            .DisableHideApplication    |   /
            .AutoHideToolbar
            .HideMenuBar               |   /
            .DisableAppleMenu          |   /
            .DisableProcessSwitching   |   /
            .DisableSessionTermination |   /
            .DisableHideApplication    |   /
            .AutoHideToolbar */
     
        let optionsDictionary = [NSFullScreenModeApplicationPresentationOptions :
            NSNumber(unsignedLong: presOptions.rawValue)]
     
        self.view.enterFullScreenMode(NSScreen.mainScreen()!, withOptions:optionsDictionary)
        self.view.wantsLayer = true
    }


I was able to find this source code from the following link:

http://blogs.wcode.org/2015/06/howto-create-a-locked-down-fullscreen-cocoa-application-and-implement-nslayoutconstraints-using-swift/


Again, thanks for the help and the point in the right direction and I hope that this can help others.