How it works
DismissImmersiveSpaceAction is the action type that closes the immersive space your app is currently presenting, returning the wearer to the surrounding Shared Space or to your app's windowed content. You don't construct it yourself; SwiftUI vends a ready-made instance through the environment, and you invoke that instance to tear down the active immersive scene. Reach for it whenever a view inside an immersive experience needs to programmatically end that experience — for example, an "Exit" control, a completion handler, or a state change that should send the person back out of the fully immersive context. Because dismissing a scene is asynchronous, the action is awaitable, giving you a defined point at which the space has finished closing.
Read the action from the environment with @Environment(\.dismissImmersiveSpace)
SwiftUI publishes a configured
DismissImmersiveSpaceActionthrough the environment, so you obtain it by declaring an environment property rather than initializing the struct directly. In the example,@Environment(\.dismissImmersiveSpace) private var dismissImmersiveSpacebinds the property to that vended instance, and every view in the hierarchy shares the same correctly-scoped action.Call the action like a function
DismissImmersiveSpaceActiondefines acallAsFunctionmember, which lets you invoke the stored value as if it were a method. The example writesdismissImmersiveSpace()— that call expression is what triggers the dismissal of the currently open immersive space; you never namecallAsFunctionexplicitly.Await the dismissal inside an asynchronous context
The call is
async, so it must run where you can suspend. TheButton("Exit Space")action spins up aTask { ... }and writesawait dismissImmersiveSpace(), letting the system wind down the scene off the main run loop while theawaitmarks the suspension point until the close completes.Place the trigger in a normal view and modifier chain
The action plugs into ordinary SwiftUI controls — here it lives in the closure of a
Buttonstyled with.buttonStyle(.borderedProminent)inside aVStack. The surroundingText,font, andpaddingare just the host UI; the meaningful work is theawait dismissImmersiveSpace()the button performs.
Task with await dismissImmersiveSpace(); print("space closed") so the line after the await runs only once the immersive space has fully finished dismissing.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 DismissImmersiveSpaceActionDemo: View {
@Environment(\.dismissImmersiveSpace) private var dismissImmersiveSpace
var body: some View {
VStack(spacing: 16) {
Text("Immersive Space")
.font(.headline)
Button("Exit Space") {
Task {
await dismissImmersiveSpace()
}
}
.buttonStyle(.borderedProminent)
}
.padding()
}
}