Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I hide title bar in SwiftUI? (for MacOS app)

I know how to hide the title bar with Storyboard.

enter image description here

But I can't do this in SwiftUI.

I want to hide the title bar and the control buttons and make a floating image view.

like image 205
Retriever Avatar asked Dec 27 '25 20:12

Retriever


2 Answers

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

try HiddenTitleBarWindowStyle()

like image 189
bdeviOS Avatar answered Dec 30 '25 16:12

bdeviOS


Also, if you have SwiftUI based App @main you can use use the .windowStyle() modifier to hide the title bar and AppDelegate to hide the buttons, like so:

import SwiftUI

@main
struct MyApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .windowStyle(HiddenTitleBarWindowStyle())
    }
}

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ notification: Notification) {
        hideTitleBar()
    }

    func hideTitleBar() {
        guard let window = NSApplication.shared.windows.first else { assertionFailure(); return }
        window.standardWindowButton(.closeButton)?.isHidden = true
        window.standardWindowButton(.miniaturizeButton)?.isHidden = true
        window.standardWindowButton(.zoomButton)?.isHidden = true
    }
}

And for Catalyst, the title bar could be hidden using .onAppear { } modifier and UITitleBar api:

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
    }
}
like image 21
Dannie P Avatar answered Dec 30 '25 17:12

Dannie P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!