One Logo Pool Ball
SwiftUI · IOS API REFERENCE

ViewAlignedScrollTargetBehavior

struct · iOS 17.0+ · SDK 27.1

struct ViewAlignedScrollTargetBehavior

The scroll behavior that aligns scroll targets to view-based geometry. You use this behavior when a scroll view should always align its scroll targets to a rectangle that's aligned to the geometry of a view. In the following example, the scroll view always picks an item view to settle on. ScrollView(.horizontal) { LazyHStack(spacing: 10.0) { ForEach(items) { item in ItemView(item) } } .scrollTargetLayout() } .scrollTargetBehavior(.viewAligned) .padding(.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. You can customize whether the view aligned behavior limits the number of views that can be scrolled at a time by using the ``ViewAlignedScrollTargetBehavior/LimitBehavior`` type. Provide a value of ``ViewAlignedScrollTargetBehavior/LimitBehavior/always`` to always have the behavior only allow a few views to be scrolled at a time. By default, the view aligned behavior will limit the number of views it scrolls when in a compact horizontal size class when scrollable in the horizontal axis, when in a compact vertical size class when scrollable in the vertical axis, and otherwise does not impose any limit on the number of views that can be scrolled.

Members (5)

init · iOS 26.0+

init

TypeScript
static initLimitBehaviorAnchor(limitBehavior: ViewAlignedScrollTargetBehavior_LimitBehavior, anchor?: (_SwiftUICore_UnitPoint) | null): ViewAlignedScrollTargetBehavior
Swift declaration
init(limitBehavior: ViewAlignedScrollTargetBehavior.LimitBehavior, anchor: UnitPoint?)

Creates a view aligned scroll behavior with the provided limit behavior and anchor.

init · iOS 17.0+

init

TypeScript
static initLimitBehavior(limitBehavior?: ViewAlignedScrollTargetBehavior_LimitBehavior): ViewAlignedScrollTargetBehavior
Swift declaration
init(limitBehavior: ViewAlignedScrollTargetBehavior.LimitBehavior = .automatic)

Creates a view aligned scroll behavior.

method · iOS 17.0+

updateTarget

TypeScript
updateTarget(target: _SwiftUICore_ScrollTarget, context: ScrollTargetBehaviorContext): { $ret: null; $out: { "target": _SwiftUICore_ScrollTarget } }
Swift declaration
func updateTarget(_ target: inout ScrollTarget, context: ViewAlignedScrollTargetBehavior.TargetContext)

Updates the proposed target that a scrollable view should scroll to. The system calls this method in two main cases: - When a scroll gesture ends, it calculates where it would naturally scroll to using its deceleration rate. The system provides this calculated value as the target of this method. - When a scrollable view's size changes, it calculates where it should be scrolled given the new size and provides this calculates value as the target of this method. You can implement this method to override the calculated target which will have the scrollable view scroll to a different position than it would otherwise.

Omit the output parameters from the call. The result contains $ret and $out, with target inside $out.

method · iOS 18.4+

properties

TypeScript
properties(context: ScrollTargetBehaviorPropertiesContext): ScrollTargetBehaviorProperties
Swift declaration
func properties(context: ViewAlignedScrollTargetBehavior.PropertiesContext) -> ViewAlignedScrollTargetBehavior.Properties

Properties of this behavior

init · iOS 26.0+

init

TypeScript
static initAnchor(anchor?: (_SwiftUICore_UnitPoint) | null): ViewAlignedScrollTargetBehavior
Swift declaration
init(anchor: UnitPoint?)

Creates a view aligned scroll behavior with the provided anchor.

Skipped declarations

No declarations were skipped in this snapshot.

All SwiftUI types →