MAC OS Xcode Swift 2.2 Fullscreen Mode

我们两清 提交于 2020-12-05 11:27:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!