ตัวอ่าน ScrollView และจุดยึด
เลื่อนไปยังรายการที่ระบุด้วยโปรแกรม
ตัวอ่าน ScrollView และจุดยึด เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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. 🎉
เรียนรู้ Swift ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 30
- บทเรียน
- 120
คำถามที่พบบ่อย
บทเรียน “ตัวอ่าน ScrollView และจุดยึด” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ตัวอ่าน ScrollView และจุดยึด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตัวอ่าน ScrollView และจุดยึด”
เลื่อนไปยังรายการที่ระบุด้วยโปรแกรม คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ตัวอ่าน ScrollView และจุดยึด” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ScrollView แนวนอนและแนวตั้ง
- LazyVStack และ LazyHStack
- กริดปรับตามขนาดด้วย LazyVGrid
- ตัวอ่าน ScrollView และจุดยึด