<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "visionOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/onImmersionChange(initial:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE17onImmersionChange7initial_QrSb_yAA0eF7ContextV_AGtctF"
  },
  "title" : "onImmersionChange(initial:_:)"
}
-->

# onImmersionChange(initial:_:)

Performs an action when the immersion state of your app changes.

```
nonisolated func onImmersionChange(initial: Bool = true, _ action: @escaping (ImmersionChangeContext, ImmersionChangeContext) -> Void) -> some View
```

## Parameters

`initial`

Whether the action should be run when this view initially
appears.

`action`

A closure to run when the immersion changes.

- oldValue: The value representing the old state of immersion.
- newValue: The value representing the current state of immersion.

## Discussion

Depending on the immersion style used for the Immersive Space in your
app, the amount of immersion can be controlled by actions such as
turning the Digital Crown. Use this modifier to define a closure that is
run when the immersion state changes. The following example sets the
value of a binding depending on the current amount of immersion:

```
struct ImmersiveView: View {
    @Binding var enableSoundEffects: Bool

    var body: some View {
        MyView()
            .onImmersionChange { _, newImmersion in
                guard let amount = newImmersion.amount else {
                    enableSoundEffects = false
                    return
                }
                // Enable some effects based on the updated
                // amount of immersion
                enableSoundEffects = amount > 0.5
            }
    }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)