TechnologiesSwiftUI

ImmersiveSpaceContentBuilder struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A result builder for composing a collection of immersive space elements.

How it works

ImmersiveSpaceContentBuilder is a result builder that assembles the content of an immersive space on visionOS from a sequence of declarations written as ordinary Swift statements. SwiftUI applies it to the trailing closure of an ImmersiveSpace scene, so you can list the RealityKit entities, lights, and views that make up your unbounded scene without manually constructing a content collection. Reach for it whenever you define an immersive space and want the same declarative, statement-by-statement composition you already use inside a view's body.

  1. Annotate the content closure with @ImmersiveSpaceContentBuilder

    Marking a closure or function with the attribute lets SwiftUI transform each statement in the body into a single combined piece of immersive content. In the demo, the comment in the second Text calls this out directly: @ImmersiveSpaceContentBuilder composes the visionOS scene content below., naming the builder that drives the composition.

  2. List each scene element as its own statement

    Because it is a result builder, you simply write one declaration per line and the builder collects them in order — no array or explicit return is required. The example sketches that shape with the stacked Label("Globe Entity", systemImage: "globe") and Label("Ambient Light", systemImage: "lightbulb"), standing in for the entity and light a real immersive space would contribute.

  3. Let the surrounding scene supply the builder

    You rarely name ImmersiveSpaceContentBuilder yourself; an ImmersiveSpace scene declares its content parameter with the attribute, so passing a closure is enough to opt in. Here the whole arrangement is hosted inside ImmersiveSpaceContentBuilderDemo's body, the place a view-side preview plugs into the same builder-driven content.

  4. Use control flow inside the builder

    Result builders accept if, switch, and for so scene content can vary by state or platform while still reading as a flat list. The inner VStack(spacing: 8) that groups the two Label rows shows where such conditional or repeated elements would sit, each one becoming part of the composed immersive content.

Try it — Add a third Label("Audio Source", systemImage: "speaker.wave.2") line beside the existing two labels to see the builder fold an extra element into the composed content without any other code changes.

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.

ImmersiveSpaceContentBuilder.swift
struct ImmersiveSpaceContentBuilderDemo: View {
    var body: some View {
        VStack(spacing: 12) {
            Text("Immersive Space")
                .font(.headline)
            Text("@ImmersiveSpaceContentBuilder composes the visionOS scene content below.")
                .font(.caption)
                .multilineTextAlignment(.center)
                .foregroundStyle(.secondary)
            VStack(spacing: 8) {
                Label("Globe Entity", systemImage: "globe")
                Label("Ambient Light", systemImage: "lightbulb")
            }
            .padding()
            .background(.blue.opacity(0.15), in: RoundedRectangle(cornerRadius: 12))
        }
        .padding()
    }
}
Live preview
Immersive Space @ImmersiveSpaceContentBuilder composes the visionOS scene content below. Globe Entity Ambient Light
Immersive Space @ImmersiveSpaceContentBuilder composes the visionOS scene content below. Globe Entity Ambient Light
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →