Challenge - 5 Problems
Grid Layout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
How many items are visible in this LazyVGrid?
Consider this SwiftUI code snippet that creates a vertical grid with fixed columns:
How many grid items will be shown in one full row?
let columns = [GridItem(.fixed(100)), GridItem(.fixed(100))]
var body: some View {
ScrollView {
LazyVGrid(columns: columns) {
ForEach(0..<5) { index in
Rectangle()
.frame(height: 50)
.overlay(Text("Item \(index)"))
}
}
}
}How many grid items will be shown in one full row?
iOS Swift
let columns = [GridItem(.fixed(100)), GridItem(.fixed(100))] var body: some View { ScrollView { LazyVGrid(columns: columns) { ForEach(0..<5) { index in Rectangle() .frame(height: 50) .overlay(Text("Item \(index)")) } } } }
Attempts:
2 left
💡 Hint
Count how many columns are defined in the columns array.
✗ Incorrect
The LazyVGrid uses the columns array to define how many columns appear per row. Here, there are 2 columns, so each row shows 2 items.
❓ ui_behavior
intermediate2:00remaining
What happens if you use LazyHGrid with one fixed row?
Given this SwiftUI code:
What is the layout behavior of the circles?
let rows = [GridItem(.fixed(80))]
var body: some View {
ScrollView(.horizontal) {
LazyHGrid(rows: rows) {
ForEach(0..<4) { index in
Circle()
.frame(width: 60, height: 60)
.overlay(Text("\(index)"))
}
}
}
}What is the layout behavior of the circles?
iOS Swift
let rows = [GridItem(.fixed(80))] var body: some View { ScrollView(.horizontal) { LazyHGrid(rows: rows) { ForEach(0..<4) { index in Circle() .frame(width: 60, height: 60) .overlay(Text("\(index)")) } } } }
Attempts:
2 left
💡 Hint
LazyHGrid arranges items horizontally along rows.
✗ Incorrect
LazyHGrid with one fixed row arranges items side by side horizontally in that row. The ScrollView(.horizontal) allows sideways scrolling if needed.
🧠 Conceptual
advanced2:00remaining
What is the effect of using .adaptive GridItem in LazyVGrid?
In SwiftUI, you can define grid columns like this:
What does the .adaptive option do in a LazyVGrid?
let columns = [GridItem(.adaptive(minimum: 80))]
What does the .adaptive option do in a LazyVGrid?
iOS Swift
let columns = [GridItem(.adaptive(minimum: 80))] var body: some View { LazyVGrid(columns: columns) { ForEach(0..<10) { index in Rectangle() .frame(height: 50) .overlay(Text("Item \(index)")) } } }
Attempts:
2 left
💡 Hint
Think about how many columns can fit if each column is at least 80 points wide.
✗ Incorrect
The .adaptive option creates as many columns as possible that are at least the minimum width. This makes the grid flexible and responsive.
❓ lifecycle
advanced2:00remaining
When are views inside LazyVGrid created and destroyed?
In SwiftUI, LazyVGrid creates views lazily. What does this mean for the lifecycle of the views inside the grid?
Attempts:
2 left
💡 Hint
Lazy means views are made only when needed.
✗ Incorrect
LazyVGrid creates views only when they are about to appear on screen, saving memory and improving performance. Views off screen can be destroyed.
🔧 Debug
expert2:00remaining
Why does this LazyVGrid code cause a runtime crash?
Examine this SwiftUI code:
When running, the app crashes with a layout constraint error. Why?
let columns = [GridItem(.fixed(100)), GridItem(.fixed(100))]
var body: some View {
ScrollView {
LazyVGrid(columns: columns) {
ForEach(0..<3) { index in
Text("Item \(index)")
.frame(width: 150, height: 50)
.background(Color.blue)
}
}
}
}When running, the app crashes with a layout constraint error. Why?
iOS Swift
let columns = [GridItem(.fixed(100)), GridItem(.fixed(100))] var body: some View { ScrollView { LazyVGrid(columns: columns) { ForEach(0..<3) { index in Text("Item \(index)") .frame(width: 150, height: 50) .background(Color.blue) } } } }
Attempts:
2 left
💡 Hint
Check if child views fit inside the fixed column widths.
✗ Incorrect
The columns are fixed at 100 points wide, but the child views request 150 points width, which conflicts and causes a runtime layout crash.