LazyVStack and LazyHStack help show many items in a list without slowing down your app. They only create views when needed.
LazyVStack and LazyHStack in iOS Swift
LazyVStack(alignment: .leading, spacing: 10) { // Your views here } LazyHStack(alignment: .top, spacing: 10) { // Your views here }
LazyVStack arranges views vertically, one below another.
LazyHStack arranges views horizontally, side by side.
LazyVStack {
Text("Apple")
Text("Banana")
Text("Cherry")
}LazyHStack(spacing: 20) { Image(systemName: "star") Image(systemName: "heart") Image(systemName: "moon") }
ScrollView {
LazyVStack(alignment: .center, spacing: 5) {
ForEach(0..<100) { number in
Text("Item \(number)")
}
}
}ScrollView(.horizontal) {
LazyHStack(spacing: 15) {
ForEach(0..<50) { number in
Text("\(number)")
.padding()
.background(Color.blue.opacity(0.3))
.cornerRadius(8)
}
}
}This app shows a vertical scrollable list of 20 items using LazyVStack. Each item is styled with padding and background color.
import SwiftUI struct ContentView: View { var body: some View { ScrollView { LazyVStack(alignment: .leading, spacing: 12) { ForEach(1...20, id: \.self) { number in Text("Item number \(number)") .font(.headline) .padding() .background(Color.yellow.opacity(0.3)) .cornerRadius(10) } } .padding() } } } @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
Lazy stacks create views only when they appear on screen, saving memory and improving speed.
Use LazyVStack for vertical lists and LazyHStack for horizontal lists inside ScrollView.
Common mistake: Using VStack or HStack for many items can slow down your app because all views are created at once.
LazyVStack and LazyHStack help show many items efficiently by creating views only when needed.
Use them inside ScrollView to make smooth scrolling lists.
They save memory and keep your app fast with large lists.
Practice
LazyVStack or LazyHStack in SwiftUI?Solution
Step 1: Understand Lazy Stacks behavior
LazyVStack and LazyHStack create views only when they are about to appear on screen, saving memory.Step 2: Compare with normal stacks
Normal VStack or HStack create all views immediately, which can be slow for large lists.Final Answer:
To create stacks that load views only when they appear on screen -> Option CQuick Check:
Lazy stacks load views lazily = D [OK]
- Thinking lazy stacks load all views at once
- Confusing lazy stacks with animation features
- Assuming lazy stacks disable scrolling
Solution
Step 1: Check ForEach usage with range and id
ForEach needs an identifiable range; using 1...5 with id: \.self is correct.Step 2: Verify Text label inside loop
Text("Item \(i)") correctly shows labels from Item 1 to Item 5.Final Answer:
LazyVStack with ForEach(1...5, id: \.self) and Text("Item \(i)") -> Option BQuick Check:
Correct ForEach syntax with id = A [OK]
- Omitting id parameter in ForEach with ranges
- Using incorrect range syntax
- Forgetting to add 1 when using 0..<5 for labels
ScrollView {
LazyHStack {
ForEach(1...3, id: \.self) { i in
Text("Number \(i)")
}
}
}Solution
Step 1: Understand ScrollView with LazyHStack
ScrollView by default scrolls vertically, but LazyHStack arranges views horizontally.Step 2: Check ForEach output
ForEach creates Text views labeled Number 1, Number 2, Number 3 in a horizontal line.Final Answer:
A horizontal row showing: Number 1 Number 2 Number 3 -> Option DQuick Check:
LazyHStack arranges horizontally = B [OK]
- Assuming LazyHStack arranges views vertically
- Thinking LazyHStack cannot be inside ScrollView
- Confusing LazyVStack and LazyHStack behavior
ScrollView {
LazyVStack {
ForEach(0..<5) { i in
Text("Item \(i)")
}
}
}Solution
Step 1: Check ForEach usage with range
ForEach with range 0..<5 requires an id parameter to identify each element uniquely.Step 2: Confirm other parts are correct
LazyVStack inside ScrollView is valid; Text syntax is correct; range 0..<5 is valid.Final Answer:
Missing id parameter in ForEach causes a compile error -> Option AQuick Check:
ForEach with range needs id = A [OK]
- Forgetting id parameter in ForEach with ranges
- Thinking LazyVStack can't be inside ScrollView
- Misreading range syntax as invalid
Solution
Step 1: Understand lazy loading for large lists
LazyHStack inside ScrollView creates views only when needed, saving memory for large lists.Step 2: Compare with eager loading
HStack or VStack loads all views immediately, which is slow and memory-heavy for 1000 images.Step 3: Confirm horizontal scrolling
LazyHStack arranges items horizontally and works well with horizontal ScrollView.Final Answer:
Use ScrollView with LazyHStack and load images lazily inside ForEach -> Option AQuick Check:
LazyHStack + ScrollView for large horizontal lists = C [OK]
- Using HStack loads all views immediately
- Confusing vertical and horizontal stacks
- Using List when horizontal scrolling is needed
Ready to prove it?
Pass the test and this pattern moves unknown
