Hide Title Bar in SwiftUI App for mac

How can I hide the Title Bar in the new SwiftUI App Protocol?

Since the AppDelegate.swift and SceneDelegate.swift protocols are gone, I cant follow this documentation anymore:
https://developer.apple.com/documentation/uikit/mac_catalyst/removing_the_title_bar_in_your_mac_app_built_with_mac_catalyst

I can't implement this code:
Code Block
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        #if targetEnvironment(macCatalyst)
        if let titlebar = windowScene.titlebar {
            titlebar.titleVisibility = .hidden
            titlebar.toolbar = nil
        }
        #endif
    }
}


Hope it's still possible with the new AppProtocol..

Thank in advance

Replies

It can be done simply by adding a modifier to the WindowGroup, like this

Code Block
import SwiftUI
@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }.windowStyle(HiddenTitleBarWindowStyle())
    }
}


@vdkdamian
For iOS, you can still request UIKit life cycle when you create a SwiftUI App. But not for Multiplatform.
Code Block
.windowStyle(HiddenTitleBarWindowStyle())

will not work because that API is not available for iOS app with a macOS running destination.
@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
#if canImport(AppKit)
		.windowStyle(.hiddenTitleBar)
#endif
    }
}

For Catalyst, you can reach to your scene in .onAppear modifier, like so:

import SwiftUI

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onAppear { hideTitleBarOnCatalyst() }
        }
    }

    func hideTitleBarOnCatalyst() {
#if targetEnvironment(macCatalyst)
        (UIApplication.shared.connectedScenes.first as? UIWindowScene)?.titlebar?.titleVisibility = .hidden
#endif
    }
}

And if needed, you still have access to the AppDelegate protocol:

import SwiftUI

@main
struct MyApp: App {
    @UIApplicationDelegateAdaptor private var appDelegate: MyAppDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class MyAppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        return true
    }
}

More details here: https://developer.apple.com/documentation/swiftui/uiapplicationdelegateadaptor