ScrollView Reader & Anchors
Scroll to specific items programmatically.
ScrollView Reader & Anchors is a free SwiftUI Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Jumping to Content
Sometimes you need to scroll to a specific item in code, like a Back to Top button. ScrollViewReader makes that possible. 🎯
Wrap the ScrollView
Place a ScrollViewReader around your ScrollView; it hands you a proxy you use to control scrolling.
ScrollViewReader { proxy in
ScrollView {
Content()
}
}Tag Each Item
Give scrollable items a unique id so the reader knows exactly where to jump when you ask it to.
ForEach(items) { item in
Row(item).id(item.id)
}Call scrollTo
The proxy has one key method: scrollTo. Pass an id and the ScrollView glides that item into view.
proxy.scrollTo(42)Animate the Jump
Wrap the call in withAnimation so the scroll glides smoothly instead of snapping instantly to the target.
withAnimation {
proxy.scrollTo(42)
}Anchor the Position
The anchor argument decides where the item lands: .top pins it to the top, .center to the middle.
proxy.scrollTo(42, anchor: .top)Back to Top Button
A common pattern is a button that calls scrollTo on the first item id to whisk the user back up.
Button("Top") {
proxy.scrollTo(firstID, anchor: .top)
}Jump on Appear
Call scrollTo inside onAppear to open a long list already scrolled to the user latest unread row.
.onAppear {
proxy.scrollTo(lastReadID)
}IDs Must Match
If scrollTo does nothing, check that the id you pass exactly matches an id set on a visible item.
Pair with State
Trigger scrolling from a @State change, like a search result selection, to guide the user automatically.
onChange(of: selected) { id in
proxy.scrollTo(id)
}Effortless Navigation
With ScrollViewReader and anchors, long screens feel navigable and friendly instead of an endless swipe. ✨
Quick Check
Think about how you move to a specific row.
Recap
You learned to use ScrollViewReader, tag items with ids, and call scrollTo with anchors to jump anywhere in code. 🎉
Frequently asked questions
Is the “ScrollView Reader & Anchors” lesson free?
Yes — the full text of “ScrollView Reader & Anchors” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “ScrollView Reader & Anchors”?
Scroll to specific items programmatically. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “ScrollView Reader & Anchors” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Horizontal & Vertical ScrollView
- LazyVStack & LazyHStack
- Adaptive Grids with LazyVGrid
- ScrollView Reader & Anchors