How to transition between a window and an immersive space
What if we want to hide a window when entering an immersive space?
Short version: Opening an immersive space is an asynchronous operation. We should wait for it to complete before closing our window.
await openImmersiveSpace(id: "GardenScene")
dismissWindow(id: "MainWindow")Longer version: We’ll expand on our scene phase example. We’ll build a transition between the window and space. Then, we’ll transition back to the window when a condition is met. In this example, we tan tap on five bubbles to exit the space.
Content View has a button. This button will toggle the immersive space based on scene phase state. We store this state in the app model.
Button(action: {
Task {
if(appModel.gardenOpen) {
await dismissImmersiveSpace()
return
} else if (!appModel.gardenOpen) {
await openImmersiveSpace(id: "GardenScene")
dismissWindow(id: "MainWindow")
}
}
}, label: {
Text(appModel.gardenOpen ? "Close Immersive Space" :"Open Immersive Space")
})If the space is open, we close it. Otherwise we open the space–awaiting for completion–then dismiss the main window.
How about going from the space to the window? The first thing to point out is that openWindow is not asynchronous.

In theory, we should be able to open the window, then close the scene. But I’ve run into edge cases where this fails silently and the user is kicked out of the app. The space was closed before the main window was reopened. This may be a bug in visionOS 2, but I’m not able to reliably reproduce it. It seems to happen only sometimes.
Remember that each scene in our example tracks its own scene phase and updates our app model. We can check this value from the immersive space. Once it has been set to true we can dismiss the space.
Task {
openWindow(id: "MainWindow")
// Start checking if window is open.
while !appModel.mainWindowOpen {
try? await Task.sleep(nanoseconds: 100_000_000) // 0.1 seconds
}
// Once the main window has opened, dismiss immersive space
await dismissImmersiveSpace()
}Video demo showing a simple swap transition between a window and an immersive space.
Sample code is available in Garden18 in the Step Into Example Projects repo.
Support our work so we can continue to bring you new examples and articles.
Download the Xcode project with this and many more examples from Step Into Vision.
Some examples are provided as standalone Xcode projects. You can find those here.

Follow Step Into Vision