Skip to content
Commitwise
Menu

View identity (SwiftUI)

Also called: Structural identity, Explicit identity

How SwiftUI decides a view is the same view as before: by its position in the code or by an explicit id.

Definition

Views with the same identity keep their state and animate their changes; a new identity means the old view is removed and a new one inserted, with transitions, and its state starts over. ForEach uses the elements' ids, and the id modifier sets identity explicitly.

In practice

Changing .id(item.version) on a detail view resets its scroll position and state when a new version arrives.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.