- Published on
- 1 min read
> How to make a SwiftUI List scroll automatically?
When you have a List or ScrollView in SwiftUI and you want to scroll to a specific item automatically, you can use the ScrollViewReader and ScrollViewProxy to achieve this. Wrap your ScrollView in a ScrollViewReader and access the proxy provided to its content closure. As long as your list elements are Hashable, you can simple tell the proxy which element to scroll to.
struct ContentView: View {
@State private var colors: [Color] = [
.red, .orange, .yellow, .green, .blue, .indigo, .purple
]
var body: some View {
ScrollViewReader { proxy in
ScrollView {
LazyVStack {
Button("Scroll to bottom") {
withAnimation {
proxy.scrollTo(colors[colors.endIndex - 1])
}
}
ForEach(colors, id: \.self) { color in
color.frame(
height: 200
)
}
}
}
}
}
}
You can optionally provide the scrollTo method with an anchor to specify where the item should be positioned after scrolling.
proxy.scrollTo(colors[colors.endIndex - 1], anchor: .center)
// Continue_Learning
Building a Pull-to-Load-More ScrollView in SwiftUI
How to detect when users scroll to the bottom of a list and automatically load more content.
SwiftUI Animations: From Basics to KeyframeAnimator
A practical guide to SwiftUI animations covering implicit and explicit animations, spring physics, PhaseAnimator for multi-step sequences, and KeyframeAnimator for timeline-based control.
How to Change the Background Color of a View in SwiftUI
Learn different ways to set background colors on SwiftUI views, from simple color fills to gradients and materials.
// Stay Updated
Get notified when I publish new tutorials on Swift, SwiftUI, and iOS development. No spam, unsubscribe anytime.