ScrollView Reader y anchors
Desplácese programáticamente hasta elementos concretos.
ScrollView Reader y anchors es una lección gratuita de SwiftUI Academy en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de SwiftUI Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de SwiftUI Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. 🎉
Preguntas frecuentes
¿La lección «ScrollView Reader y anchors» es gratis?
Sí — el texto completo de «ScrollView Reader y anchors» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de SwiftUI Academy, actualiza a CoddyKit PRO. El curso de SwiftUI Academy incluye 4 lecciones en total.
¿Qué aprenderé en «ScrollView Reader y anchors»?
Desplácese programáticamente hasta elementos concretos. Practicas SwiftUI Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar SwiftUI Academy?
No se requiere experiencia previa. SwiftUI Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «ScrollView Reader y anchors»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de SwiftUI Academy?
Sí. Cada lección de SwiftUI Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- ScrollView horizontal y vertical
- LazyVStack y LazyHStack
- Grids adaptativas con LazyVGrid
- ScrollView Reader y anchors