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.
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
Textcalls this out directly:@ImmersiveSpaceContentBuilder composes the visionOS scene content below., naming the builder that drives the composition.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")andLabel("Ambient Light", systemImage: "lightbulb"), standing in for the entity and light a real immersive space would contribute.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'sbody, the place a view-side preview plugs into the same builder-driven content.Use control flow inside the builder
Result builders accept
if,switch, andforso scene content can vary by state or platform while still reading as a flat list. The innerVStack(spacing: 8)that groups the twoLabelrows shows where such conditional or repeated elements would sit, each one becoming part of the composed immersive content.
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.
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()
}
}