0
0
iOS Swiftmobile~20 mins

Grid layout (LazyVGrid, LazyHGrid) in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Grid Layout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
How many items are visible in this LazyVGrid?
Consider this SwiftUI code snippet that creates a vertical grid with fixed columns:
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)"))
      }
    }
  }
}
A2
B1
C5
D10
Attempts:
2 left
💡 Hint
Count how many columns are defined in the columns array.
ui_behavior
intermediate
2:00remaining
What happens if you use LazyHGrid with one fixed row?
Given this SwiftUI code:
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)"))
      }
    }
  }
}
ACircles are arranged vertically in a single column, scrollable up and down.
BCircles are arranged in a grid with 4 rows and 1 column.
CCircles overlap each other in the same position.
DCircles are arranged horizontally in a single row, scrollable sideways.
Attempts:
2 left
💡 Hint
LazyHGrid arranges items horizontally along rows.
🧠 Conceptual
advanced
2:00remaining
What is the effect of using .adaptive GridItem in LazyVGrid?
In SwiftUI, you can define grid columns like this:
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)"))
    }
  }
}
AIt fixes the grid to exactly one column of 80 points width.
BIt creates as many columns as fit horizontally with at least 80 points width each.
CIt creates columns that automatically resize to fill the entire screen width.
DIt creates a grid with a fixed number of columns equal to 80.
Attempts:
2 left
💡 Hint
Think about how many columns can fit if each column is at least 80 points wide.
lifecycle
advanced
2: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?
AViews are created only when they appear on screen and destroyed when scrolled off screen.
BAll views are created immediately when the grid appears and stay in memory.
CViews are created randomly regardless of visibility.
DViews are created once and never destroyed even if off screen.
Attempts:
2 left
💡 Hint
Lazy means views are made only when needed.
🔧 Debug
expert
2:00remaining
Why does this LazyVGrid code cause a runtime crash?
Examine this SwiftUI code:
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)
      }
    }
  }
}
AText views cannot have background colors inside LazyVGrid.
BForEach range is too small to fill the grid, causing a crash.
CThe fixed column width (100) conflicts with the child frame width (150), causing layout errors.
DScrollView must be horizontal for LazyVGrid to work.
Attempts:
2 left
💡 Hint
Check if child views fit inside the fixed column widths.