简体   繁体   中英

SwiftUI: Using enterFullScreenMode on MacOS

I've been using window.toggleFullScreen() to present a full-screen window, but I've found that this won't work on an iPad being used as a separate display. The app won't transition on this screen - just gets a beep without any error being presented in the debug window (would appreciate any insights about this first problem:)

Code snippet for this transition:

  window.collectionBehavior = [] 
  window.toggleFullScreen(self)
  • Frame resizes are handled by the delegates

I'm now trying to use window.contentView ?.enterFullScreenMode.

This transitions ok (even on the iPad) but I am missing the menu and dock .

Code below:

// AppDelegate
var window: NSWindow!

// applicationDidFinishLaunching()
window = NSWindow(contentRect: device.size, styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], backing: .buffered, defer: false)
self.window.delegate = self
let contentView = ContentViewv11().frame(minWidth: 100, maxWidth: .infinity, minHeight: 50, maxHeight: .infinity)
window.contentView = NSHostingView(rootView: contentView.environmentObject(display).  ....etc

// initiated via menu item action:
let presOptions: NSApplication.PresentationOptions = [.autoHideMenuBar, .autoHideDock]
let optionsDictionary = [NSView.FullScreenModeOptionKey.fullScreenModeApplicationPresentationOptions: presOptions]
window.contentView?.enterFullScreenMode(NSScreen.main!, withOptions: optionsDictionary)
window.contentView?.wantsLayer = true \\tried also with this line removed

I haven't seen an example where contentView is being used with the enterFullScreenMode method. This is giving me a completely blank window apart from the app itself. No hidden dock or menu bar. Users of the app will need at least the menu bar. Help appreciated.

I have tried some attempts using Swift 2 calling window.contentView?.enterFullScreenMode from within a Scene displaying a single Window. This was worse, I just got a blank screen and the Mac was locked up. Calling.exitFullScreenMode or even.toggleFullScreen wouldn't bring it back.

I found the cause of my original problem with.toggleFullScreen, during the transition to full screen I was calling window.setFrame. The call was coming from the delegate methods with resizing. Using.setFrame during the transition worked fine on a normal monitor but not using sidecar with the iPad. Pausing the.setFrame until windowDidEnterFullScreen() delegate was called fixed the problem.

It looks to me that.toggleFullScreen is the method to use with SwiftUI for the moment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM