swift2 Mac OS Xcode Swift 2.2全屏幕模式

rn0zuynd  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(179)

什么样的Swift代码会将应用程序切换到全屏?我找到了IOS示例代码的参考。我正在寻找一个适用于MacOS应用程序的代码。

sz81bmfz

sz81bmfz1#

针对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
}
np8igboo

np8igboo2#

一种方法是覆盖NSViewController中的viewDidAppear

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()

bcs8qyzn

bcs8qyzn3#

如果您想要不同的行为,当您将鼠标移到顶部时,菜单栏是可用的。然而,它开始时是一个正常大小的窗口,然后增长,所以这可能不是理想的取决于你正在做什么。

override func viewDidAppear() {
    view.window?.toggleFullScreen(self)
}

相关问题