In SwiftUI, what changes when you replace a VStack with a LazyVStack inside a ScrollView, and when should you keep the VStack?
answer
- when each child view is built
- what the stack knows about sizes
- Apple: start eager, profile first
- one of them takes pinnedViews
basics
~20 sA VStack inside a ScrollView creates and lays out every child at once; a LazyVStack creates rows only as they are about to render. Keep the VStack for modest content: Apple advises switching only when profiling shows a gain.
solid answer
~40 sNeither stack scrolls by itself; the `ScrollView` does. A `VStack` builds all of its children when it is displayed, so it knows every size up front and lays out precisely, but a thousand rich rows cost a thousand views at load. A `LazyVStack` creates a row only when it needs to render it and computes geometry only as rows become visible; Apple describes this as trading some layout correctness for performance. Apple's advice is to start with a standard stack and switch only when Instruments shows a worthwhile improvement. Lazy stacks also take `pinnedViews` for sticky section headers and footers. Laziness is about creation: the documentation does not promise that rows scrolled away are released, so heavy rows still need care.
code
swift · 32 linesimport SwiftUI
struct DaySection: Identifiable {
let id: Date
let title: String
let posts: [String]
}
struct DayGroupedFeed: View {
let days: [DaySection]
var body: some View {
ScrollView {
LazyVStack(alignment: .leading, spacing: 8, pinnedViews: [.sectionHeaders]) {
ForEach(days) { day in
Section {
ForEach(day.posts, id: \.self) { post in
Text(post)
.padding(.horizontal)
}
} header: {
Text(day.title)
.font(.headline)
.frame(maxWidth: .infinity, alignment: .leading)
.padding()
.background(.bar)
}
}
}
}
}
}go deeper
Recall that a ScrollView provides scrolling, a VStack builds every child at once, and a LazyVStack builds rows only as they come on screen.
Explain on-demand creation, why geometry is only known for visible rows, the constant-views-per-element rule for ForEach, and pinnedViews for sticky headers.
Show how you would prove a stack change with Instruments, and separate creation cost from memory held by rows that have already scrolled away.
Frame the default your team should use: eager stacks for bounded screens, lazy stacks for long data, with profiling evidence required before switching either way.
## Two stacks, one scroll view In SwiftUI a **stack** arranges children in a line, and a **`ScrollView`** provides the scrollable viewport. Stacks have no scrolling of their own, lazy or not: Apple's article on lazy stacks says to wrap them in a `ScrollView`. The question is therefore not "which one scrolls" but **when each child view gets created and measured**. - **`VStack`** (and `HStack`, `ZStack`) loads its whole view hierarchy when it is displayed. Every child is created, measured and placed before the first frame. - **`LazyVStack`** (and `LazyHStack`, available since iOS 14) "doesn't create items until it needs to render them onscreen", in the words of its documentation page. Apple's scroll-view overview adds that a lazy container loads views when they are "visible or almost visible". ## What happens as the reader scrolls 1. The `ScrollView` proposes its size to the stack. 2. A `VStack` has already built every row, so it knows the exact content height. 3. A `LazyVStack` builds only the rows that intersect, or nearly intersect, the visible region, and **calculates geometry only for rows as they become visible**. 4. As the reader scrolls, more rows are created on demand; `ForEach` inside a lazy container is queried lazily too. Apple's "Creating performant scrollable stacks" article profiles a horizontal row of 1,000 profile cards: with `HStack`, all 1,000 `ProfileView` instances load at once; with `LazyHStack`, only the four visible ones start. | | `VStack` in `ScrollView` | `LazyVStack` in `ScrollView` | |---|---|---| | **When children are created** | All at once, when displayed | On demand, near the viewport | | **Layout knowledge** | Every size known up front | Sizes known only for visible rows | | **Cost at load** | Grows with the row count | Roughly the visible rows | | **Sticky section headers** | Not available | `pinnedViews: [.sectionHeaders]` | | **Apple's default advice** | Start here | Switch when profiling shows a gain | ## Side effects you can observe - **Appearance callbacks.** In a `VStack`, every row is created at load, so an `onAppear` on the last row runs straight away. In a `LazyVStack` it runs only when the reader gets near the end, which is what makes "load more" triggers work. - **Approximate layout.** Because a lazy stack measures rows only as they appear, the total content height is an estimate until rows have been seen. Apple calls this trading "some degree of layout correctness for performance". - **Constant views per element.** Apple's `ForEach` documentation notes that `List` and `LazyVStack` query a `ForEach` lazily, and that for maximal performance each element should produce a **constant number of views**. An `if` inside the `ForEach` body that yields zero or one view defeats that; filter the data instead. - **Pinned headers.** Only the lazy stacks (and lazy grids) take `pinnedViews`. With `Section` headers and `pinnedViews: [.sectionHeaders]`, a feed's date headers stay attached to the top while their section scrolls; in a `LazyHStack` headers pin to the leading edge. ## When to keep the VStack Apple's guidance on choosing containers is direct: **always start with a standard stack** and switch to a lazy one only if profiling shows a worthwhile improvement. Keep a `VStack` when: - the content is a bounded, modest number of views, such as a settings-like screen or a profile header with a few sections; - you need precise layout of the whole content, for example to read the exact content height; - rows are cheap and created once. Move to a `LazyVStack` when the data is long or unbounded (a feed, a timeline, search results) or each row is expensive (images, nested stacks), and confirm the gain with the SwiftUI template in Instruments on a real device, as Apple's article recommends. ## Memory: what laziness does not promise Laziness governs **creation**. Apple's pages say a lazy stack creates items only as needed; they do not say that rows which scrolled away are released. Engineers who see memory climbing in a long feed often assume laziness is broken. The practical rules: - keep per-row state small and keep large data in the model, not in each row's `@State`; - decode images at display size rather than holding full-resolution bitmaps per row; - stop expensive work, such as video playback, when a row leaves the screen, for example with `onScrollVisibilityChange(threshold:_:)` on iOS 18; - measure with Instruments before and after, rather than trusting either stack blindly.
- Does a LazyVStack release rows after they scroll off screen?Apple's documentation promises only that a lazy stack does not create items until it needs to render them; it makes no promise about releasing them. Treat offscreen rows as possibly still alive: keep row state small, decode images at display size, and stop heavy work such as video playback when a row leaves the screen. Confirm the effect with Instruments instead of assuming.
- Why can an if statement inside a ForEach hurt a LazyVStack?Apple's ForEach documentation says List and LazyVStack query a ForEach lazily and perform best when each element produces a constant number of views. An if that yields zero or one view per element breaks that, so SwiftUI cannot count rows without building them. Filter the array before the ForEach instead.
- Where do pinned headers attach in a LazyHStack?In a LazyVStack, pinned section headers attach to the top and footers to the bottom. In a LazyHStack inside a horizontal ScrollView, headers attach to the leading edge and footers to the trailing edge, according to Apple's article on grouping data with lazy stacks.
An eager VStack is a buffet laid out before the doors open: every dish is ready and its place on the table is known, but you paid for all of it up front. A LazyVStack is a kitchen that cooks each dish as a table orders it; nothing about that arrangement promises the plates are cleared as soon as the guests leave.
- A LazyVStack scrolls by itself, so it needs no ScrollView.
- LazyVStack recycles a fixed pool of row views like a table view.
- Lazy stacks are always faster, so every VStack should become one.
- A VStack inside a ScrollView only builds the rows that are on screen.
- Rows a LazyVStack has built are guaranteed to be freed when offscreen.