Set immersion style at runtime

Hello, is it possible to change the immersion style at runtime?

Answered by Vision Pro Engineer in 789800022

Yes it is possible using the immersionStyle method.

Here's a snippet to illustrate this:

import SwiftUI

@main
struct DynamicImmersionApp: App {
    
    @Environment(\.openImmersiveSpace) var openImmersiveSpace
    @Environment(\.dismissImmersiveSpace) var dismissImmersiveSpace
    
    @State private var immersionStyle:ImmersionStyle = .mixed
    @State private var showImmersiveSpace = false

    var body: some SwiftUI.Scene {
        WindowGroup {
            VStack {
                if showImmersiveSpace {
                    Button("Dismiss immersion") {
                        showImmersiveSpace = false
                    }
                }
                else {
                    Button("Show mixed immersion") {
                        immersionStyle = .mixed
                        showImmersiveSpace = true
                    }
                    
                    Button("Show full immersion") {
                        immersionStyle = .full
                        showImmersiveSpace = true
                    }
                }
            }
            .onChange(of: showImmersiveSpace) { _, newValue in
                Task {
                    // simplified for brevity
                    print(newValue)
                    if newValue {
                        await openImmersiveSpace(id: "ImmersiveSpace")
                    } else {
                        await dismissImmersiveSpace()
                    }
                }
            }
        }
        
        ImmersiveSpace(id: "ImmersiveSpace") {
            ImmersiveView()
        }.immersionStyle(selection: .constant(immersionStyle), in: immersionStyle)
    }
}

Yes it is possible using the immersionStyle method.

Here's a snippet to illustrate this:

import SwiftUI

@main
struct DynamicImmersionApp: App {
    
    @Environment(\.openImmersiveSpace) var openImmersiveSpace
    @Environment(\.dismissImmersiveSpace) var dismissImmersiveSpace
    
    @State private var immersionStyle:ImmersionStyle = .mixed
    @State private var showImmersiveSpace = false

    var body: some SwiftUI.Scene {
        WindowGroup {
            VStack {
                if showImmersiveSpace {
                    Button("Dismiss immersion") {
                        showImmersiveSpace = false
                    }
                }
                else {
                    Button("Show mixed immersion") {
                        immersionStyle = .mixed
                        showImmersiveSpace = true
                    }
                    
                    Button("Show full immersion") {
                        immersionStyle = .full
                        showImmersiveSpace = true
                    }
                }
            }
            .onChange(of: showImmersiveSpace) { _, newValue in
                Task {
                    // simplified for brevity
                    print(newValue)
                    if newValue {
                        await openImmersiveSpace(id: "ImmersiveSpace")
                    } else {
                        await dismissImmersiveSpace()
                    }
                }
            }
        }
        
        ImmersiveSpace(id: "ImmersiveSpace") {
            ImmersiveView()
        }.immersionStyle(selection: .constant(immersionStyle), in: immersionStyle)
    }
}
Set immersion style at runtime
 
 
Q