问题
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.
回答1:
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
}
}
↳ Apple Developer API Reference : viewDidAppear()
回答2:
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
}
回答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)
}
来源:https://stackoverflow.com/questions/38164793/mac-os-xcode-swift-2-2-fullscreen-mode