TechnologiesSwiftUI

Window struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A scene that presents its content in a single, unique window.

How it works

Window is a Scene you declare in your App to present a single, unique window for a particular purpose — an inspector, a tool palette, a game board, or any surface that should never have duplicates. Unlike WindowGroup, which lets people open many interchangeable copies of the same interface, a Window is created at most once; opening it again brings the existing instance forward rather than spawning a new one. Reach for Window when a piece of your app's UI is inherently singular and you want the system to manage its uniqueness, ordering, and lifetime for you.

  1. Declare the scene in your App's body

    A Window is a Scene, so it lives in the body of a type conforming to App alongside your other scenes. You construct it with a title and a unique identifier and hand it a view builder for its content, as in Window("Inspector", id: "inspector") { ContentView() }. The system guarantees only one instance of that scene exists at a time.

  2. Give it a stable identity with the id parameter

    The id argument ("inspector" in the example) is the string you use elsewhere to refer to this exact window. Because the identity is fixed, it is what enforces the single-instance behavior and what other parts of your app target when they need to show or hide the window.

  3. Provide the content view

    The trailing view builder supplies what the window hosts — here that role is filled by ContentView(). The view you return (the VStack with the macwindow Image and the "Inspector" Text in the runnable sample) is ordinary SwiftUI: the Window scene simply gives it a place to live and a frame to fill.

  4. Open and dismiss it programmatically

    Because the window has a known id, you drive its visibility from anywhere using the openWindow and dismissWindow environment actions, passing the same identifier — for example openWindow(id: "inspector"). Calling openWindow when the window is already open activates the existing instance instead of creating a second one.

  5. Attach scene modifiers to shape its behavior

    As a Scene, a Window accepts scene-level modifiers such as defaultPosition(_:), defaultSize(_:), keyboardShortcut(_:), and commands(content:), letting you set where it first appears, how large it opens, and which menu items toggle it. These refine the single window without changing the content view it hosts.

Try it — Add a button to ContentView whose action calls openWindow(id: "inspector") twice in a row, then confirm only one Inspector window appears — the second call reactivates the existing instance instead of opening a duplicate.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

Window.swift
struct WindowDemo: View {
    var body: some View {
        // `Window` is a single-instance Scene declared in an App:
        //   Window("Inspector", id: "inspector") { ContentView() }
        // This view is the content such a Window would host.
        VStack(spacing: 12) {
            Image(systemName: "macwindow")
                .font(.system(size: 44))
                .foregroundStyle(.tint)
            Text("Inspector")
                .font(.headline)
            Text("Hosted in a single-instance Window scene")
                .font(.caption)
                .foregroundStyle(.secondary)
                .multilineTextAlignment(.center)
        }
        .padding()
    }
}
Live preview
Inspector Hosted in a single-instance Window scene
Inspector Hosted in a single-instance Window scene
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →