defaultScrollAnchor

Associates an anchor to control which part of the scroll view's content should be rendered by default.

View ModifieriOS 17.0+macOS 14.0+tvOS 17.0+watchOS 10.0+

Overloads

2 ways to call compose().defaultScrollAnchor(). Each is one SwiftUI overload, picked by the arguments you pass.

React
compose().defaultScrollAnchor(anchor: SwiftUI.UnitPoint | null)
Swift
nonisolated func defaultScrollAnchor(_ anchor: UnitPoint?) -> some View

Parameters

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.