TechnologiesSwiftUIScrolling

ScrollAnchorRole struct

iOSmacOStvOSwatchOSvisionOSiOS 18.0+✓ renders

A type defining the role of a scroll anchor.

How it works

ScrollAnchorRole describes which aspect of a scroll view's behavior a content anchor governs. A scroll view can have more than one notion of an anchor at the same time — the point it scrolls to by default and the point it keeps pinned when content grows or shrinks — and this type lets you name which of those you mean. You reach for ScrollAnchorRole when you pass an anchor to a modifier like defaultScrollAnchor(_:for:), using the role to disambiguate whether your UnitPoint should drive initial alignment or size-change stabilization.

  1. Choose a role to qualify an anchor

    ScrollAnchorRole is a small set of constants, each identifying one job an anchor can perform. Rather than constructing a value directly, you select the appropriate case at the call site where an anchor is needed, as with .alignment in .defaultScrollAnchor(.bottom, for: .alignment).

  2. Pass the role through the for: parameter

    Anchor modifiers accept a ScrollAnchorRole through their for: argument, binding the supplied UnitPoint to that specific behavior. In the example, for: .alignment tells the scroll view that the .bottom anchor controls where content is positioned initially, so the list opens scrolled to its last message.

  3. Use .alignment to set the resting position

    The .alignment role makes the anchor decide how content lines up within the scroll view's visible region when there is no explicit scroll offset. Combined here with .bottom, it anchors the ForEach of Text("Message \(i)") rows to the bottom edge on first appearance.

  4. Use .sizeChanges to hold a point steady

    The companion .sizeChanges role keeps a chosen anchor point fixed as the content's size changes, so newly inserted or removed items don't visually shift what the reader is looking at. Swapping the role on the same .defaultScrollAnchor call repurposes the .bottom anchor from initial placement to growth stabilization.

  5. Apply it once on the scroll view

    Because ScrollAnchorRole is consumed by a modifier on the scroll view itself, you attach it where the ScrollView is declared rather than on individual rows. The single .defaultScrollAnchor(.bottom, for: .alignment) here configures anchoring for the entire scrolling region.

Try it — Change for: .alignment to for: .sizeChanges in .defaultScrollAnchor(.bottom, for: .alignment) to see the anchor pin the bottom of the list as content grows instead of merely choosing the opening position.

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.

ScrollAnchorRole.swift
struct ScrollAnchorRoleDemo: View {
    var body: some View {
        ScrollView {
            VStack(spacing: 12) {
                ForEach(1..<21) { i in
                    Text("Message \(i)")
                        .frame(maxWidth: .infinity)
                        .padding()
                        .background(.blue.opacity(0.15))
                        .cornerRadius(8)
                }
            }
            .padding()
        }
        .defaultScrollAnchor(.bottom, for: .alignment)
    }
}
Live preview
Message 1 Message 2 Message 3 Message 4 Message 5 Message 6 Message 7 Message 8 Message 9 Message 10 Message 11 Message 12 Message 13 Message 14 Message 15 Message 16 Message 17 Message 18 Message 19 Message 20
Message 1 Message 2 Message 3 Message 4 Message 5 Message 6 Message 7 Message 8 Message 9 Message 10 Message 11 Message 12 Message 13 Message 14 Message 15 Message 16 Message 17 Message 18 Message 19 Message 20
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →