TechnologiesSwiftUI

DismissImmersiveSpaceAction struct

iOSmacOStvOSwatchOSvisionOS✓ renders

An action that dismisses an immersive space.

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.

  1. Read the action from the environment with @Environment(\.dismissImmersiveSpace)

    SwiftUI publishes a configured DismissImmersiveSpaceAction through the environment, so you obtain it by declaring an environment property rather than initializing the struct directly. In the example, @Environment(\.dismissImmersiveSpace) private var dismissImmersiveSpace binds the property to that vended instance, and every view in the hierarchy shares the same correctly-scoped action.

  2. Call the action like a function

    DismissImmersiveSpaceAction defines a callAsFunction member, which lets you invoke the stored value as if it were a method. The example writes dismissImmersiveSpace() — that call expression is what triggers the dismissal of the currently open immersive space; you never name callAsFunction explicitly.

  3. Await the dismissal inside an asynchronous context

    The call is async, so it must run where you can suspend. The Button("Exit Space") action spins up a Task { ... } and writes await dismissImmersiveSpace(), letting the system wind down the scene off the main run loop while the await marks the suspension point until the close completes.

  4. 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 Button styled with .buttonStyle(.borderedProminent) inside a VStack. The surrounding Text, font, and padding are just the host UI; the meaningful work is the await dismissImmersiveSpace() the button performs.

Try it — Replace the body of the 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.

DismissImmersiveSpaceAction.swift
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()
    }
}
Live preview
Immersive Space Exit Space
Immersive Space Exit Space
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →