ScrollTargetBehavior

A type that defines the scroll behavior of a scrollable view.

ProtocoliOS 17.0+macOS 14.0+tvOS 17.0+watchOS 10.0+ScrollTargetBehavior in SwiftUI

Declaration

React
type ScrollTargetBehavior =
  | 'paging'
  | 'viewAligned'
  | { viewAligned: { anchor: UnitPoint | null; }; }
  | { viewAligned: { limitBehavior: ViewAlignedScrollTargetBehaviorLimitBehavior; }; }
  | { viewAligned: { limitBehavior: ViewAlignedScrollTargetBehaviorLimitBehavior; anchor: UnitPoint | null; }; }
  | PagingScrollTargetBehavior
  | ViewAlignedScrollTargetBehavior;
Swift
protocol ScrollTargetBehavior

Pass a literal, or build one with SwiftUI.ScrollTargetBehavior from import { SwiftUI } from 'swiftui-react-native'.

Type properties

Type methods

Overview

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.

From Apple’s SwiftUI documentation. Code samples there are Swift.

Used by

See also

More in Layout.