ViewAlignedScrollTargetBehavior
The scroll behavior that aligns scroll targets to view-based geometry.
Declaration
type ViewAlignedScrollTargetBehavior =
| { anchor: UnitPoint | null; }
| { limitBehavior: ViewAlignedScrollTargetBehaviorLimitBehavior; anchor: UnitPoint | null; }
| { limitBehavior?: ViewAlignedScrollTargetBehaviorLimitBehavior; };struct ViewAlignedScrollTargetBehaviorType methods
Overview
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.
From Apple’s SwiftUI documentation. Code samples there are Swift.
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.