ScrollTargetBehavior
A type that defines the scroll behavior of a scrollable view.
Declaration
type ScrollTargetBehavior =
| 'paging'
| 'viewAligned'
| { viewAligned: { anchor: UnitPoint | null; }; }
| { viewAligned: { limitBehavior: ViewAlignedScrollTargetBehaviorLimitBehavior; }; }
| { viewAligned: { limitBehavior: ViewAlignedScrollTargetBehaviorLimitBehavior; anchor: UnitPoint | null; }; }
| PagingScrollTargetBehavior
| ViewAlignedScrollTargetBehavior;protocol ScrollTargetBehaviorPass a literal, or build one with SwiftUI.ScrollTargetBehavior from import { SwiftUI } from 'swiftui-react-native'.
Type properties
'paging'SwiftUI.ScrollTargetBehavior.pagingThe scroll behavior that aligns scroll targets to container-based geometry.
'viewAligned'SwiftUI.ScrollTargetBehavior.viewAlignedThe scroll behavior that aligns scroll targets to view-based geometry.
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.
- AlignmentAn alignment in both axes.
- ContainerBackgroundPlacementThe placement of a container background.
- ContainerValuesReadableKeys of SwiftUI ContainerValues JavaScript can read.
- ContainerValuesWritableKeys of SwiftUI ContainerValues JavaScript can write.
- ControlSizeThe size classes, like regular or small, that you can apply to controls within a view.
- DocumentLaunchGeometryProxyA proxy for access to the frame of the scene and its title view.
- DynamicTypeSizeA Dynamic Type size, which specifies how large scalable content should be.
- EdgeAn enumeration to indicate one edge of a rectangle.
- EdgeInsetsThe inset distances for the sides of a rectangle.
- EdgeInsetsChainEdgeInsets with chained calls: SwiftUI.EdgeInsets.inset(...).
- EdgeSetAn efficient set of edges.
- GeometryProxyA proxy for access to the size and coordinate space (for anchor resolution) of the container view.