Grades Adaptáveis com LazyVGrid
Disponha cartões em colunas de grade flexíveis.
Grades Adaptáveis com LazyVGrid é uma aula grátis de SwiftUI Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de SwiftUI Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de SwiftUI Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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. 🎉
Aprenda Swift com um tutor de IA — grátis
Escreva e execute código real no seu navegador, obtenha ajuda instantânea de um tutor de IA 24/7 e continue de onde parou na web ou no app.
- Cursos
- 30
- Aulas
- 120
Perguntas Frequentes
A aula “Grades Adaptáveis com LazyVGrid” é grátis?
Sim — o texto completo de “Grades Adaptáveis com LazyVGrid” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de SwiftUI Academy, atualize para CoddyKit PRO. O curso de SwiftUI Academy inclui 4 aulas no total.
O que vou aprender em “Grades Adaptáveis com LazyVGrid”?
Disponha cartões em colunas de grade flexíveis. Você pratica SwiftUI Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar SwiftUI Academy?
Nenhuma experiência prévia é necessária. SwiftUI Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Grades Adaptáveis com LazyVGrid”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de SwiftUI Academy?
Sim. Cada aula de SwiftUI Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- ScrollView Horizontal e Vertical
- LazyVStack e LazyHStack
- Grades Adaptáveis com LazyVGrid
- Leitor e Âncoras de ScrollView