ScrollView
A scrollable view.
Declaration
struct ScrollView<Content> where Content : ViewMentioned in
Overview
The scroll view displays its content within the scrollable content region. As the user performs platform-appropriate scroll gestures, the scroll view adjusts what portion of the underlying content is visible. ScrollView can scroll horizontally, vertically, or both, but does not provide zooming functionality.
In the following example, a ScrollView allows the user to scroll through a VStack containing 100 Text views. The image after the listing shows the scroll view’s temporarily visible scrollbar at the right; you can disable it with the showsIndicators parameter of the ScrollView initializer.
var body: some View {
ScrollView {
VStack(alignment: .leading) {
ForEach(0..<100) {
Text("Row \($0)")
}
}
}
}[Image]
Controlling Scroll Position
You can influence where a scroll view is initially scrolled by using the defaultScrollAnchor(_:) view modifier.
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)Or provide an alignment of `UnitPoint/bottom`` to have the scroll view start at the bottom of its content when a scroll view is scrollable in its vertical axes.
ScrollView {
// initially bottom aligned content
}
.defaultScrollAnchor(.bottom)After the scroll view initially renders, the user may scroll the content of the scroll view.
To perform programmatic scrolling, wrap one or more scroll views with a ScrollViewReader.