What Swift code will switch the app to fullscreen? I found references with example code for IOS. I am looking for a code which works for a MacOS app.
Asked
Active
Viewed 3,069 times
3 Answers
10
Updated for Swift 4
override func viewDidAppear() {
let presOptions: NSApplication.PresentationOptions = [.fullScreen, .autoHideMenuBar]
let optionsDictionary = [NSView.FullScreenModeOptionKey.fullScreenModeApplicationPresentationOptions: presOptions]
view.enterFullScreenMode(NSScreen.main!, withOptions: optionsDictionary)
view.wantsLayer = true
}

possen
- 8,596
- 2
- 39
- 48
-
1This opens full screen modes nicely, but I note that when I put a label into the view, the label does not appear. Same with buttons. Why? – Peter Wiley Jan 28 '19 at 12:41
-
same issue showing the black window. Any solution? – Krunal Nagvadia Jan 27 '21 at 13:51
4
One way is to override viewDidAppear
in NSViewController
:
class ViewController : NSViewController {
override func viewDidAppear() {
let presOptions: NSApplicationPresentationOptions = ([.FullScreen,.AutoHideMenuBar])
let optionsDictionary = [NSFullScreenModeApplicationPresentationOptions :
NSNumber(unsignedLong: presOptions.rawValue)]
self.view.enterFullScreenMode(NSScreen.mainScreen()!, withOptions:optionsDictionary)
self.view.wantsLayer = true
}
}

l'L'l
- 44,951
- 10
- 95
- 146
3
An alternative, if you want different behavior, where the menu bar is available when you move your mouse to top is this. However, it starts out as a normal size window then grows, so that may not be desirable depending on what you are doing.
override func viewDidAppear() {
view.window?.toggleFullScreen(self)
}

possen
- 8,596
- 2
- 39
- 48
-
That's much better answer, because in the accepted one the aplication in fullscreen locks everything in MacOS, you can't resize thw window, there is no menu bar appearing and even Command + Tab keys won't work! If you change system volume the indicator won't display either! – Heitor Jun 01 '19 at 03:54
-