SwiftUI Fullscreen Mac os

How do I make a SwiftUI view fullscreen running Mac OS, Catalina or Big Sur?

Replies

OK got it!

First start the Xcode project with LifeCycle -> AppKit App Delegate, not SwiftUI App.

In the AppDelegate.swift file add:

    let mainScreen: NSScreen = NSScreen.screens[0]
    window.contentView?.enterFullScreenMode(mainScreen)

at the end of func applicationDidFinishLaunching. Looks like this:

class AppDelegate: NSObject, NSApplicationDelegate
{
  var window: NSWindow!

  func applicationDidFinishLaunching(_ aNotification: Notification)
  {
    // Create the SwiftUI view that provides the window contents.
    let contentView = ContentView()

    // Create the window and set the content view.
    window = NSWindow(
        contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
        styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
        backing: .buffered, defer: false)
    window.makeKeyAndOrderFront(self)
    window.isReleasedWhenClosed = false
    window.center()
    window.setFrameAutosaveName("Main Window")
    window.contentView = NSHostingView(rootView: contentView)
Code Block
// start App in fullScreen mode
    let mainScreen: NSScreen = NSScreen.screens[0]
    window.contentView?.enterFullScreenMode(mainScreen)
  }

  func applicationWillTerminate(_ aNotification: Notification)
  {
    // Insert code here to tear down your application
  }
}


And finally set the background color of contentView to white, otherwise it will look dirty gray.

Would be nice to switch fullScreen on and off for different views, still digging.

  • Is there a way to do this in SwiftUI?

  • This seems a partial answer to the question: https://stackoverflow.com/questions/23896803/os-x-detecting-when-front-app-goes-into-fullscreen-mode

Add a Comment

For a SwiftUI macOS App, I came up with this solution to enter full screen. In my use case I want the app to go full screen immediately on launch, but you can do this at any point.

import SwiftUI

@main
struct MacApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onAppear {
                    DispatchQueue.main.asyncAfter(0.1) {
                        if let window = NSApplication.shared.windows.last {
                            window.toggleFullScreen(nil)
                        }
                    }
                }
        }
    }
}