defaultScrollAnchor
Associates an anchor to control which part of the scroll view's content should be rendered by default.
Overloads
2 ways to call compose().defaultScrollAnchor(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().defaultScrollAnchor(anchor: SwiftUI.UnitPoint | null)nonisolated func defaultScrollAnchor(_ anchor: UnitPoint?) -> some ViewParameters
anchor*SwiftUI.UnitPoint | null
Discussion
Use this modifier to specify an anchor to control both which part of the scroll view's content should be visible initially and how the scroll view handles content size changes.
Provide a value of UnitPoint.center to have the scroll
view start in the center of its content when a scroll view
is scrollable in both axes.
ScrollView([.horizontal, .vertical]) {
// initially centered content
}
.defaultScrollAnchor(.center)Provide a value of UnitPoint.bottom to have the scroll view
start at the bottom of its content when scrollable in the
vertical axis.
@Binding var items: [Item]
@Binding var scrolledID: Item.ID?
ScrollView {
LazyVStack {
ForEach(items) { item in
ItemView(item)
}
}
}
.defaultScrollAnchor(.bottom)The user may scroll away from the initial defined scroll position. When the content size of the scroll view changes, it may consult the anchor to know how to reposition the content.
See also
More in Layout.
- alignmentGuideSets the view's vertical alignment.
- aspectRatioConstrains this view's dimensions to the specified aspect ratio.
- containerCornerOffsetAdjusts the view's layout to avoid the container view's corner insets for the specified edges.
- containerRelativeFramePositions this view within an invisible frame with a size relative to the nearest container.
- controlSizeSets the size for controls within this view.
- dynamicTypeSizeSets the Dynamic Type size within the view to the given value.
- fixedSizeFixes this view at its ideal size.
- flipsForRightToLeftLayoutDirectionSets whether this view mirrors its contents horizontally when the layout direction is right-to-left.
- framePositions this view within an invisible frame.
- geometryGroupIsolates the geometry (e.g. position and size) of the view from its parent view.
- gridCellAnchorSpecifies a custom alignment anchor for a view that acts as a grid cell.
- gridCellColumnsTells a view that acts as a cell in a grid to span the specified number of columns.