TechnologiesSwiftUI

SceneBuilder struct

iOSmacOStvOSwatchOSvisionOSiOS 14.0+✓ renders

A result builder for composing a collection of scenes into a single

How it works

SceneBuilder is the result builder that SwiftUI applies to the body of a type conforming to App, letting you list the scenes that make up your application's user interface as a sequence of expressions rather than constructing a combined value by hand. Because the property is annotated @SceneBuilder by the App protocol, you simply write one scene after another and the builder assembles them into the single Scene that the runtime expects. Reach for it implicitly whenever you declare an app entry point and need to declare more than one window, settings panel, or document presentation, and explicitly when you want to factor scene-producing code into a helper or apply conditional logic across scenes.

  1. Let the App protocol supply @SceneBuilder

    The body requirement of App is already marked with @SceneBuilder, so the closure you write is transformed by the builder without any extra annotation. In a type like the demo's App entry point, body returns some Scene, and SceneBuilder is what turns the scenes you list inside it into that conforming result; the demo's SceneBuilderDemo view is the kind of content such a scene ultimately presents.

  2. List Scene values as statements

    Each top-level expression in a SceneBuilder closure must be a Scene, and the builder combines them in order. You write scene types such as WindowGroup, Settings, and DocumentGroup directly one after another, mirroring how the demo's explanatory Text describes SceneBuilder as the tool that composes WindowGroup, Settings, and DocumentGroup inside an App's body.

  3. Compose multiple scenes without explicit grouping

    Like ViewBuilder for views, SceneBuilder supports listing several scenes side by side and merges them through its buildBlock methods, so a single body can host a primary WindowGroup alongside a Settings scene with no wrapper container. This is the multi-scene composition the demo names when it lists WindowGroup, Settings, and DocumentGroup together.

  4. Apply scene modifiers to each element

    Modifiers that return a Scene — such as commands(_:), defaultSize(_:), or windowStyle(_:) — chain onto the individual scenes you place in the builder, and the result is still a valid SceneBuilder statement. You attach them to a scene the same way the demo attaches view modifiers like .font and .foregroundStyle to its Text and Image, keeping each scene's configuration local to that scene.

  5. Use control flow with buildEither and buildOptional

    SceneBuilder provides buildOptional and the buildEither pair so you can guard a scene behind a platform check or an if/else and still satisfy some Scene. This lets you, for example, include a DocumentGroup only on platforms that support it, choosing between scenes the way the demo's VStack chooses which subviews to stack.

Try it — In the app body that hosts SceneBuilderDemo, add a second scene after the existing WindowGroup — for example Settings { SceneBuilderDemo() } — and watch SceneBuilder accept both scenes as a single some Scene with no wrapper required.

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.

SceneBuilder.swift
struct SceneBuilderDemo: View {
    var body: some View {
        VStack(spacing: 12) {
            Image(systemName: "macwindow.on.rectangle")
                .font(.largeTitle)
                .foregroundStyle(.tint)
            Text("SceneBuilder")
                .font(.headline)
            Text("A result builder used in an App's body to compose Scenes like WindowGroup, Settings, and DocumentGroup.")
                .font(.caption)
                .foregroundStyle(.secondary)
                .multilineTextAlignment(.center)
        }
        .padding()
    }
}
Live preview
SceneBuilder A result builder used in an App's body to compose Scenes like WindowGroup, Settings, and DocumentGroup.
SceneBuilder A result builder used in an App's body to compose Scenes like WindowGroup, Settings, and DocumentGroup.
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →