How do I make a translucent window in SwiftUI?

Asked on 08/04/2024

1 search

To make a translucent window in SwiftUI, you can use the containerBackground modifier to replace the window background color with a material that gives it a translucent effect. Here's an example from the session "Tailor macOS windows with SwiftUI":

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
            .frame(width: 300, height: 200)
            .background(Material.ultraThin)
    }
}

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .windowStyle(.plain)
                .windowLevel(.floating)
                .windowBackground(Material.ultraThin)
        }
    }
}

In this example, the Material.ultraThin is used to give the window a frosted glass effect, allowing the color from the desktop to shine through. This is demonstrated in the session Tailor macOS windows with SwiftUI.

For more details on customizing macOS windows with SwiftUI, you can refer to the session "Tailor macOS windows with SwiftUI" which covers various aspects of window customization, including styling window toolbars, refining window behaviors, and adjusting window placement.