skip to content

In a SwiftUI LazyVGrid, how do GridItem's fixed, flexible and adaptive sizes differ, and how many columns does each produce?

level: middleimportance: must knowfreq 62%

answer

  1. the columns array describes the grid
  2. one item, one column, with one exception
  3. a share of leftover space
  4. as many as fit at the minimum

basics

~20 s

In a LazyVGrid's columns array, a fixed GridItem is one column of exact width, a flexible one takes a clamped share of leftover space, and an adaptive one packs in as many minimum-width cells as fit.

solid answer

~40 s

A `LazyVGrid` can't measure columns from cells it hasn't created, so I describe them with `GridItem` values. `.fixed(80)` is one column exactly 80 points wide on every device. `.flexible(minimum:maximum:)`, the default for `GridItem()`, is one column that shares the space the fixed columns leave, clamped between 10 points and unbounded unless I say otherwise. `.adaptive(minimum:maximum:)` is the odd one: a single item fills its space with as many cells of at least the minimum as fit and stretches them, so a recipe grid shows two columns on an iPhone and more on an iPad without any code. `GridItem`'s `spacing` is the gap to the next column, while the grid's own `spacing:` separates rows, and the whole grid goes inside a `ScrollView` because it doesn't scroll by itself.

code

swift · 22 lines
swift
import SwiftUI

struct RecipeCardGrid: View {
    let titles: [String]

    private let columns = [
        GridItem(.adaptive(minimum: 160), spacing: 16)
    ]

    var body: some View {
        ScrollView {
            LazyVGrid(columns: columns, spacing: 16) {
                ForEach(titles, id: \.self) { title in
                    Text(title)
                        .frame(maxWidth: .infinity, minHeight: 120)
                        .background(.quaternary, in: RoundedRectangle(cornerRadius: 12))
                }
            }
            .padding()
        }
    }
}

go deeper

for a junior

Recall the three GridItem sizes, that GridItem() is flexible, and that a lazy grid needs a ScrollView around it to scroll.

for a middle

Explain how adaptive packs several cells into one item's space, which spacing value controls columns and which controls rows, and why a lazy grid needs columns described up front.

for a senior

Show how you would pick an adaptive minimum that works from the smallest iPhone to the largest iPad, and how you would verify the result on real widths.

for a principal

Discuss when a design system should standardise a card width and let columns follow it, rather than fixing column counts per device class.

## The columns array is the layout A `LazyVGrid` is a vertically growing grid that creates its cells only as SwiftUI needs to display them. Because it never has every cell in hand, it cannot measure a column by looking at all the cells in it. Instead you describe the columns up front with an array of `GridItem` values passed to `init(columns:alignment:spacing:pinnedViews:content:)`. The views in the `content` closure then flow into those columns from the leading edge to the trailing edge, row after row. `LazyHGrid` mirrors this: its `rows:` array describes rows, and the grid grows sideways. Each `GridItem` carries three properties: - `size`: a `GridItem.Size` value, one of `.fixed`, `.flexible` or `.adaptive`. - `spacing`: the spacing to the next item; `nil` uses the default spacing. - `alignment`: how a view sits inside its cell; `nil` uses the grid's alignment. The initializer is `GridItem(_ size: GridItem.Size = .flexible(), spacing: CGFloat? = nil, alignment: Alignment? = nil)`, so `GridItem()` on its own is a flexible column. ## The three sizes | Size | Declaration | Width of each column | Columns per item | |---|---|---|---| | Fixed | `.fixed(80)` | exactly the value | one | | Flexible | `.flexible(minimum: 10, maximum: .infinity)` | a share of the leftover space, clamped | one | | Adaptive | `.adaptive(minimum: 160, maximum: .infinity)` | as many as fit at the minimum, then stretched | one or more | **Fixed** is the simplest: the column has that width on every device. It suits a thumbnail column or an icon gutter, and it is the wrong choice for the main content because it does not grow when the screen does. **Flexible** columns share whatever space the fixed columns leave. Two `GridItem(.flexible())` values give two equal columns at any width. The `minimum` (10 points by default) and `maximum` (unbounded by default) clamp each column's share. The column count is still exactly the number of items in the array. **Adaptive** is the size that changes the column count. A single `GridItem(.adaptive(minimum: 160))` takes the space of one flexible item but places as many cells of at least 160 points into it as will fit, then lets them grow toward `maximum` to fill the row. Make the grid wider, by rotating the device or running full screen on an iPad, and more columns appear; make it narrower and columns disappear. ## Spacing and alignment Two different spacing values are in play, and mixing them up is a common cause of uneven gutters: - `GridItem`'s `spacing` is the gap after that column, before the next one. - The grid's own `spacing:` parameter is the gap along the axis the grid grows on: between rows of a `LazyVGrid`, between columns of a `LazyHGrid`. An item's `alignment` positions a cell's content when the content is smaller than the cell. Most card layouts sidestep it by giving the card `.frame(maxWidth: .infinity)` so it fills its cell. ## Choosing a size 1. Start from the content. Cards with a natural minimum width, such as recipe cards or photos, want an **adaptive** item. 2. Use **flexible** items when the design fixes the column count, such as a two-column summary or a keypad. 3. Use **fixed** only for content with a genuinely fixed width, usually beside a flexible column, as in `[GridItem(.fixed(60)), GridItem(.flexible())]`. ## Things that surprise people - **A lazy grid does not scroll on its own.** Apple's container guide notes that SwiftUI grids don't include a scrolling viewport, so you wrap the grid in a `ScrollView`. - **Item count equals column count only for fixed and flexible sizes.** One adaptive item can produce many columns. - **A flexible column does not size itself to its widest cell.** That is what the non-lazy `Grid` does, because it creates all of its cells up front. - **The adaptive minimum is a floor for every cell.** Set it too low and a large iPad shows a wall of narrow columns; set it too high and a small iPhone drops to a single column. - **There is no column span in a lazy grid.** `gridCellColumns(_:)` is documented for cells in a `Grid`'s `GridRow`; a full-width heading for a lazy grid usually goes in a section header or outside the grid.

  • How would you put a fixed-width thumbnail column next to a text column that takes the rest of the width?
    Use two items: `[GridItem(.fixed(60)), GridItem(.flexible())]`. The fixed item reserves exactly 60 points for the thumbnail on every device, and the flexible item takes the remaining width. Each row of the grid then holds one thumbnail and one text cell, so the content closure should emit views in that pair order, typically from a `ForEach` that produces both views per element.
  • Why does a LazyVGrid with adaptive columns not scroll when the cards overflow the screen?
    SwiftUI's grids, lazy or not, don't include a scrolling viewport. A `LazyVGrid` only arranges and lazily creates its cells; scrolling comes from wrapping it in a `ScrollView`. Without that, the grid is laid out in whatever space its parent offers and the extra rows are simply off screen. Inside a `ScrollView`, the lazy grid creates cells as they come into view.
  • Can one cell of a LazyVGrid span two columns?
    Not with a modifier. `gridCellColumns(_:)` is documented for cells inside a `GridRow` of the non-lazy `Grid`, which knows all of its cells. A lazy grid flows each item into the next single slot, so a full-width heading is normally placed in a section header or outside the grid, and a truly mixed-span layout calls for `Grid` or a custom layout.

Adaptive columns work like seating guests along a bench where everyone needs at least a set amount of room: a longer bench seats more people, and whatever space is left over is shared out so nobody sits at the bare minimum.

  • The number of GridItems always equals the number of columns, even with adaptive sizes.
  • A flexible column makes itself exactly as wide as its widest cell.
  • A LazyVGrid scrolls on its own, so no ScrollView is needed.
  • Fixed columns grow on iPad because the screen is bigger.
  • The LazyVGrid spacing parameter sets the gap between columns.