TechnologiesSwiftUI

WindowGroup struct

iOSmacOStvOSwatchOSvisionOSiOS 14.0+✓ renders

A scene that presents a group of identically structured windows.

How it works

WindowGroup is a Scene that manages a group of identically structured windows, presenting your view hierarchy as the primary user-facing surface of an app. You place it in the body of your App to declare the root content the system shows when the app launches — and, on platforms that support multiple windows, to let people open additional copies of that same interface. Reach for WindowGroup whenever you need a standard, data-driven main window rather than a single fixed window or a document-based scene.

  1. Declare the scene in your App's body

    A WindowGroup is a Scene, so it belongs in the body of a type conforming to App. You construct it with a @ViewBuilder content closure that returns the root view for each window in the group. In this example, the content shown by that closure is the WindowGroupDemo view returned from body — the VStack of an Image, a title Text, and a subtitle is exactly what would sit inside WindowGroup { ... }.

  2. Supply the root content closure

    The trailing closure you pass to WindowGroup defines a template that the system instantiates once per window. Every window the group creates renders a fresh copy of this hierarchy with its own independent state. Here that template is the VStack(spacing: 12) content — labeled in the comment as the root scene WindowGroup presents.

  3. Let the group create and restore windows

    Because WindowGroup represents a group rather than a lone window, the system can present multiple windows from the same definition and restore them across launches. On macOS the standard File > New command and window restoration are handled for you; on iPadOS the group backs multiple scenes. The single Main Window rendered by this content is one such instance of the group's template.

  4. Add an optional title and identifier

    WindowGroup offers initializers that take a window title and a string id, letting you name the group and target it programmatically with environment actions like openWindow. The Text("Main Window") here stands in for the kind of titled root content you would label and, if needed, address by identifier when opening new instances.

Try it — Wrap a second copy of the VStack in its own window by giving WindowGroup an id: and calling openWindow(id:) from a button, then watch the Text("Main Window") content appear in a brand-new, independently-stated window.

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.

WindowGroup.swift
struct WindowGroupDemo: View {
    var body: some View {
        // A WindowGroup presents this root content as the app's main window.
        // WindowGroup { ContentView() } in an App; here is that content.
        VStack(spacing: 12) {
            Image(systemName: "macwindow")
                .font(.system(size: 44))
                .foregroundStyle(.tint)
            Text("Main Window")
                .font(.title2.bold())
            Text("Root scene shown by WindowGroup")
                .font(.subheadline)
                .foregroundStyle(.secondary)
        }
        .padding()
    }
}
Live preview
Main Window Root scene shown by WindowGroup
Main Window Root scene shown by WindowGroup
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →