กริดปรับตามขนาดด้วย LazyVGrid
จัดวางการ์ดในคอลัมน์กริดที่ยืดหยุ่น
กริดปรับตามขนาดด้วย LazyVGrid เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why a Grid?
Stacks give you rows or columns, but a LazyVGrid arranges items into neat rows and columns, like a photo album. 🖼️
Define the Columns
A LazyVGrid needs a columns array of GridItem values that describe how many tracks to lay out.
let columns = [GridItem(), GridItem()]
LazyVGrid(columns: columns) {
ForEach(items) { Cell($0) }
}Flexible Items
A GridItem(.flexible()) stretches to share space evenly, so two flexible items make two equal columns.
let columns = [
GridItem(.flexible()),
GridItem(.flexible())
]Fixed Width Tracks
Use GridItem(.fixed(80)) when you want a column to stay exactly 80 points wide no matter the screen.
let columns = [GridItem(.fixed(80))]Truly Adaptive
The magic option is .adaptive(minimum:): it fits as many columns as will fit, adjusting to any screen size.
let columns = [
GridItem(.adaptive(minimum: 100))
]Always Inside a Scroll
Wrap your LazyVGrid in a ScrollView so the grid can grow past the screen and load cells lazily.
ScrollView {
LazyVGrid(columns: columns) {
ForEach(items) { Cell($0) }
}
}Spacing Between Cells
Tune the gaps with a spacing argument on the grid and on each GridItem for tidy, balanced cells.
LazyVGrid(columns: columns, spacing: 16) {
ForEach(items) { Cell($0) }
}Cells Are Just Views
Each item in the ForEach becomes a cell, so any view works: an image, a card, or a labeled button.
ForEach(photos) { photo in
Image(photo.name)
.resizable()
}Lazy by Name
Like other lazy containers, LazyVGrid builds cells as they scroll in, so large galleries stay smooth.
Horizontal Cousin
Need columns of fixed rows scrolling sideways? LazyHGrid is the horizontal counterpart with the same GridItem API.
Responsive Layouts
One adaptive grid looks great on a phone and an iPad, since columns reflow automatically to fill the space. ✨
Quick Check
Think about which GridItem reacts to screen size.
Recap
You learned to build flexible, fixed, and adaptive grids with LazyVGrid inside a ScrollView for responsive layouts. 🎉
เรียนรู้ Swift ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 30
- บทเรียน
- 120
คำถามที่พบบ่อย
บทเรียน “กริดปรับตามขนาดด้วย LazyVGrid” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “กริดปรับตามขนาดด้วย LazyVGrid” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “กริดปรับตามขนาดด้วย LazyVGrid”
จัดวางการ์ดในคอลัมน์กริดที่ยืดหยุ่น คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “กริดปรับตามขนาดด้วย LazyVGrid” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ScrollView แนวนอนและแนวตั้ง
- LazyVStack และ LazyHStack
- กริดปรับตามขนาดด้วย LazyVGrid
- ตัวอ่าน ScrollView และจุดยึด