swift - 使用 Swift 3 在 macOS 10.12 的第二个屏幕上使用 NSWindow 全屏显示?

标签 swift fullscreen nswindow

我正在尝试全屏显示第二个屏幕上的第二个窗口,但我仍然看到菜单栏。

这是我的代码:

let second_screen = NSScreen.screens()?[1]
let window = NSWindow(contentRect: (second_screen?.frame)!, styleMask: .fullScreen, backing: .buffered, defer: true, screen: second_screen)
self.window_controller = NSWindowController(window: window)
window.collectionBehavior = .fullScreenAuxiliary
window_controller?.showWindow(self)
window.toggleFullScreen(true)

有没有人对此有任何好运?

编辑 1:

我添加了这一行:

NSApp.presentationOptions = [.fullScreen, .hideDock, .autoHideMenuBar]

我收到了这个警告:

setPresentationOptions called with NSApplicationPresentationFullScreen when there is no visible fullscreen window; this call will be ignored.

编辑 2:

我切换到这个方法:

HOWTO: Create a Locked Down Fullscreen Cocoa Application and Implement NSLayoutConstraints using Swift

let presOptions: NSApplicationPresentationOptions = [.hideDock, .hideMenuBar]
let optionsDictionary = [NSFullScreenModeApplicationPresentationOptions: NSNumber(value: presOptions.rawValue)]
self.fullscreen_view?.enterFullScreenMode((NSScreen.screens()?[1])!, withOptions: optionsDictionary)

View 变成全屏但不显示内容,只是灰色。

编辑 3:

我关闭了 NSFullScreenModeAllScreens,现在它可以正常工作了:

let optionsDictionary = [NSFullScreenModeApplicationPresentationOptions: NSNumber(value: presOptions.rawValue), NSFullScreenModeAllScreens: false]

现在唯一的问题是菜单栏和停靠栏不会出现在第一个屏幕上。

最佳答案

对我来说,enterFullScreenMode 似乎只有在 View “显示”后调用时才能正常工作。

如果我在 func applicationDidBecomeActive(_ notification: Notification) {/* here */} 中调用它(或者也可以使用 Timer.scheduledTimer) .

关于swift - 使用 Swift 3 在 macOS 10.12 的第二个屏幕上使用 NSWindow 全屏显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44372887/

相关文章:

ios - 在 UILabel Word Wrapped 属性后添加 '...'

ios - 使用两个选项实现设置的 UI 最佳实践

android - 如何在布局编辑器中模拟沉浸式模式

cocoa - 创建一个带有红色、黄色和绿色按钮的标准 NSwindow

objective-c - 自定义全屏 NSWindow 中奇怪的大小调整?

swift - 具有默认实现的 UITableViewDataSource 扩展

swift - 如何在 SwiftUI 列表中重新加载数据?

c# - 全屏WP通用应用

html - 使用 css 缩小背景图像以填充屏幕

cocoa - windowWillReturnFieldEditor 从未调用过 NSTextView