8.8 · Preference keys and coordinate spaces
Environment values flow down the tree. Preferences flow up, which is how a child tells its ancestors about itself without a binding.
Mental model. Environment goes down, preferences go up. And every position you read is relative to some coordinate space; say which.
Talking upward
Preference keys and the reduce step.
- What is a PreferenceKey, and how do values travel from children up to ancestors?
A PreferenceKey is the upward counterpart of the environment. A child publishes a value with .preference(key: MyKey.self, value: x), and an ancestor reads the…
Measuring
Named coordinate spaces and frames.
- What are the coordinate spaces in SwiftUI, and how do you read a view’s frame?
Every position or frame is meaningful only relative to a coordinate space. SwiftUI has three kinds. .local is the view’s own space, with the origin at its…
More in SwiftUI
- 8.1 · What a View is, and how SwiftUI knows which view is which
- 8.2 · The update loop, dependency tracking and the cost of body
- 8.3 · State, Binding, Environment and Observation
- 8.4 · onAppear, task, onChange and scenes
- 8.5 · The layout negotiation, stacks, GeometryReader, safe area and custom Layout
- 8.6 · Lists, ForEach and NavigationStack
- 8.7 · Animations, PhaseAnimator, Canvas and TimelineView
- 8.9 · Previews and the macros behind SwiftUI
- 8.10 · State, bindings, Observation and environment, checked by running them
- 8.11 · The order of events in a render, and what Swift 6 isolation means for views
- 8.12 · Sharing space, alignment guides, adaptive containers and grids, measured
- 8.13 · What a ViewBuilder produces, and saving and restoring a navigation path