TechnologiesSwiftUIScrolling

ScrollTargetBehaviorPropertiesContext struct

iOSmacOStvOSwatchOSvisionOSiOS 18.4+✓ renders

The context in which a scroll target behavior can decide its properties.

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.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

Try it — Swap the .paging argument in .scrollTargetBehavior(.paging) for .viewAligned and observe how the resting position changes, since that is the property decision a ScrollTargetBehaviorPropertiesContext informs for a custom behavior.

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.

ScrollTargetBehaviorPropertiesContext.swift
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)
    }
}
Live preview
Row 0 Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9
Row 0 Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →