Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Title Bar in SwiftUI App for macCatalyst

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

like image 894
John Avatar asked Dec 10 '20 16:12

John


2 Answers

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) {
    }
}
like image 141
John Avatar answered Oct 05 '22 15:10

John


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.

like image 28
Adam Avatar answered Oct 05 '22 14:10

Adam