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.
Declare the scene in your App's body
A
WindowGroupis aScene, so it belongs in thebodyof a type conforming toApp. You construct it with a@ViewBuildercontent closure that returns the root view for each window in the group. In this example, the content shown by that closure is theWindowGroupDemoview returned frombody— theVStackof anImage, a titleText, and a subtitle is exactly what would sit insideWindowGroup { ... }.Supply the root content closure
The trailing closure you pass to
WindowGroupdefines 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 theVStack(spacing: 12)content — labeled in the comment as the root sceneWindowGrouppresents.Let the group create and restore windows
Because
WindowGrouprepresents 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 singleMain Windowrendered by this content is one such instance of the group's template.Add an optional title and identifier
WindowGroupoffers initializers that take a window title and a stringid, letting you name the group and target it programmatically with environment actions likeopenWindow. TheText("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.
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.
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()
}
}