How it works
ScrollTargetBehaviorPropertiesContext is the read-only context SwiftUI hands to a custom ScrollTargetBehavior so it can advertise the properties of the scroll it participates in. Rather than letting a behavior guess at the scrolling environment, the system supplies this structure when it evaluates a behavior's properties, describing the conditions under which the snapping decision is being made. Reach for it when you implement your own ScrollTargetBehavior and need to report behavior properties that depend on the surrounding scroll context. It is the bridge between SwiftUI's scroll machinery and the property values your behavior reports back.
Receive the context when reporting behavior properties
SwiftUI passes a ScrollTargetBehaviorPropertiesContext value into the properties evaluation of a ScrollTargetBehavior. You read from it; you never construct it yourself. This is the same evaluation path that a built-in behavior like the .paging value applied by .scrollTargetBehavior(.paging) goes through, except a custom behavior inspects the context to decide what properties to declare.
Apply a behavior through scrollTargetBehavior(_:)
A scroll target behavior, whether built-in or custom, is attached with the .scrollTargetBehavior(_:) modifier on the scroll container. In the example the modifier sits on the ScrollView and receives .paging, and it is during that behavior's evaluation that a ScrollTargetBehaviorPropertiesContext would be supplied to a custom equivalent.
Drive properties from the context
The point of ScrollTargetBehaviorPropertiesContext is to let your behavior's reported properties vary with the live scroll conditions instead of being fixed constants. A custom behavior reads the context and returns properties accordingly, so that paging-style snapping like the .paging behavior shown here can be expressed conditionally rather than hardcoded.
Let the behavior decide where scrolling settles
Once the properties are evaluated through the context, SwiftUI uses the behavior to determine the resting offset of the ScrollView. The VStack of Text rows in the example is simply the content that snaps; the ScrollTargetBehaviorPropertiesContext governs the rules the behavior follows when choosing the target.
Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct ScrollTargetBehaviorPropertiesContextDemo: View {
var body: some View {
ScrollView {
VStack(spacing: 12) {
ForEach(0..<10) { i in
Text("Row \(i)")
.frame(maxWidth: .infinity)
.padding()
.background(.blue.opacity(0.15))
.clipShape(RoundedRectangle(cornerRadius: 10))
}
}
.padding()
}
.scrollTargetBehavior(.paging)
}
}