skip to Main Content

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:

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..

Thanks in advance

2

Answers


  1. Chosen as BEST ANSWER

    This is how to hide the titlebar:

    struct ContentView: View {
        var body: some View {
            ZStack {
                Text("Example UI")
            }
            .withHostingWindow { window in
                #if targetEnvironment(macCatalyst)
                if let titlebar = window?.windowScene?.titlebar {
                    titlebar.titleVisibility = .hidden
                    titlebar.toolbar = nil
                }
                #endif
            }
        }
    }
    
    extension View {
        fileprivate func withHostingWindow(_ callback: @escaping (UIWindow?) -> Void) -> some View {
            self.background(HostingWindowFinder(callback: callback))
        }
    }
    
    fileprivate struct HostingWindowFinder: UIViewRepresentable {
        var callback: (UIWindow?) -> ()
    
        func makeUIView(context: Context) -> UIView {
            let view = UIView()
            DispatchQueue.main.async { [weak view] in
                self.callback(view?.window)
            }
            return view
        }
    
        func updateUIView(_ uiView: UIView, context: Context) {
        }
    }
    

  2. On your Scene, set .windowStyle(_:) to HiddenTitleBarWindowStyle().

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

    EDIT: Ah crap. While this API is supposedly available for Mac Catalyst according to the online documentation, it looks like it’s not actually marked as such in frameworks so you can’t use it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search