TechnologiesSwiftUI

ScrollView struct

iOSmacOStvOSwatchOSvisionOSiOS 13.0+✓ renders

A scrollable view.

How it works

A ScrollView is a container view that lets its content extend beyond the bounds of the available space and makes that overflow reachable by scrolling. Use it whenever the views you want to present may be taller or wider than the screen — long lists of text, stacks of cards, or any layout whose size you can't guarantee will fit. Unlike List, a ScrollView imposes no row styling or structure of its own: it simply measures its single content view, and if that content overflows along the scroll axis, it becomes draggable. Reach for it when you need scrolling but want full control over how the content is laid out and styled.

  1. Wrap content in a ScrollView

    ScrollView takes a single content view, supplied as a trailing closure, and provides vertical scrolling by default. Here the closure holds a VStack, so the whole stack scrolls as one unit; the ScrollView measures that stack and enables dragging only when it exceeds the visible height.

  2. Give it content taller than the viewport

    Scrolling only engages when the content overflows. The ForEach(1...20, id: \.self) builds twenty Text("Row \(i)") rows inside the VStack, producing far more height than fits on screen — which is exactly the condition that makes the ScrollView scroll rather than clip.

  3. Choose the scroll axis

    The initializer accepts an axis set as its first parameter; omitting it, as here, defaults to .vertical. Pass .horizontal for sideways scrolling or [.horizontal, .vertical] for both. The axis you pick determines which dimension of the content view the ScrollView lets overflow and drag.

  4. Lay out and pad content independently

    Because ScrollView adds no styling of its own, the appearance of each item comes entirely from the content. The per-row .padding(), .background(Color.blue.opacity(0.1)), and .cornerRadius(8), plus the outer .padding() on the VStack, all live inside the scrolled content and travel with it as you scroll.

Try it — Change ForEach(1...20, id: \.self) to ForEach(1...3, id: \.self) — with only three rows the content fits on screen and the ScrollView stops scrolling, showing that it engages only when its content overflows.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

ScrollView.swift
struct ScrollViewDemo: View {
    var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 12) {
                ForEach(1...20, id: \.self) { i in
                    Text("Row \(i)")
                        .font(.headline)
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .padding()
                        .background(Color.blue.opacity(0.1))
                        .cornerRadius(8)
                }
            }
            .padding()
        }
    }
}
Live preview
Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9 Row 10 Row 11 Row 12 Row 13 Row 14 Row 15 Row 16 Row 17 Row 18 Row 19
Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9 Row 10 Row 11 Row 12 Row 13 Row 14 Row 15 Row 16 Row 17 Row 18 Row 19
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →