One Logo Pool Ball
SwiftUI · IOS API REFERENCE

ScrollTargetBehavior

protocol · iOS 17.0+ · SDK 27.1

protocol ScrollTargetBehavior

A type that defines the scroll behavior of a scrollable view. A scrollable view calculates where scroll gestures should end using its deceleration rate and the state of its scroll gesture by default. A scroll behavior allows for customizing this logic. You define a scroll behavior using the ``ScrollTargetBehavior/updateTarget(_:context:)`` method. Using this method, you can control where someone can scroll in a scrollable view. For example, you can create a custom scroll behavior that aligns to every 10 points by doing the following: struct BasicScrollTargetBehavior: ScrollTargetBehavior { func updateTarget(_ target: inout ScrollTarget, context: TargetContext) { // Align to every 1/10 the size of the scroll view. let multiple = context.containerSize.width / 10.0 let newX = (target.rect.origin.x / multiple).rounded() * multiple target.rect.origin.x = newX } } ### Paging Behavior SwiftUI offers built in scroll behaviors. One such behavior is the ``PagingScrollTargetBehavior`` which uses the geometry of the scroll view to decide where to allow scrolls to end. In the following example, every view in the lazy stack is flexible in both directions and the scroll view will settle to container aligned boundaries. ScrollView { LazyVStack(spacing: 0.0) { ForEach(items) { item in FullScreenItem(item) } } } .scrollTargetBehavior(.paging) ### View Aligned Behavior SwiftUI also offers a ``ViewAlignedScrollTargetBehavior`` scroll behavior that will always settle on the geometry of individual views. ScrollView(.horizontal) { LazyHStack(spacing: 10.0) { ForEach(items) { item in ItemView(item) } } .scrollTargetLayout() } .scrollTargetBehavior(.viewAligned) .safeAreaPadding(.horizontal, 20.0) You configure which views should be used for settling using the ``View/scrollTargetLayout(isEnabled:)`` modifier. Apply this modifier to a layout container like ``LazyVStack`` or ``HStack`` and each individual view in that layout will be considered for alignment. Use types conforming to this protocol with the ``View/scrollTargetBehavior(_:)`` modifier.

Members (1)

method · iOS 18.4+

properties

Swift declaration
func properties(context: Self.PropertiesContext) -> Self.Properties

Properties of this behavior

Skipped declarations

5 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

ScrollTargetBehavior.paging

Static protocol requirements cannot be called through an instance.

ScrollTargetBehavior.viewAligned

Static protocol requirements cannot be called through an instance.

ScrollTargetBehavior.viewAligned(limitBehavior:)

Static protocol requirements cannot be called through an instance.

ScrollTargetBehavior.viewAligned(anchor:)

Static protocol requirements cannot be called through an instance.

ScrollTargetBehavior.viewAligned(limitBehavior:anchor:)

Static protocol requirements cannot be called through an instance.

All SwiftUI types →