SwiftUI ScrollView scrollTo no effect unless .id() on each row and scrollTo called in onChange

Domain: development · Evidence E2 — Local smoke reproduced
swiftswiftuiscrollviewscrollviewreaderscrolltofocusstate

Summary

In SwiftUI, `ScrollViewReader.scrollTo(_:anchor:)` appears to do nothing — the scroll position never moves. Happens especially in macOS sidebar-detail `HSplitVi…

Problem

In SwiftUI, ScrollViewReader.scrollTo(_:anchor:) appears to do nothing — the scroll position never moves. Happens especially in macOS sidebar-detail HSplitView layouts with a list of "records" in the left pane where you want the detail pane to scroll to the matching record's section in a long form.

Root Cause

Three independent requirements that are easy to miss:

  1. .id() is mandatory on every scrollable child. ScrollViewReader.scrollTo works by matching the id value to a child view's id modifier. Without it, the scroll view has nothing to navigate to and silently no-ops.
  2. scrollTo must be triggered outside the body/ForEach. Calling scrollTo directly in the view body (inside ScrollViewReader { ... }) is a side-effect during view evaluation — SwiftUI ignores it. You must call it from .onChange(of:), .onAppear, a button action, or a task modifier.
  3. LazyVStack / LazyHStack has known issues. Lazy containers may not report correct content offsets, causing scrollTo to overshoot, undershoot, or do nothing. Wrap in ScrollViewReader — if still flaky, use ScrollView(.vertical, showsIndicators: true) with a non-lazy VStack (acceptable for ~100 sections; for 1000+ rows, consider ScrollViewReader + explicit offset computation via GeometryReader).

Also: @FocusState can fight with scrollTo on macOS — if both set focus and scroll, the focus binding may steal the scroll on the next layout pass.

Solution

struct RecordDetailForm: View {
    let sections: [RecordSection]   // each has .id: UUID
    @Binding var selectedSectionID: UUID?
    @State private var lastScrolledID: UUID?

    var body: some View {
        ScrollViewReader { proxy in
            ScrollView(.vertical, showsIndicators: true) {
                // NOT LazyVStack — proxy.scrollTo works reliably with VStack
                VStack(alignment: .leading, spacing: 20) {
                    ForEach(sections) { section in
                        SectionView(section: section)
                            .id(section.id)   // ← MANDATORY: scrollTo finds this
                    }
                }
                .padding()
            }
            // Call scrollTo from onChange, never from inside body/ForEach
            .onChange(of: selectedSectionID) { oldValue, newValue in
                guard let id = newValue, id != lastScrolledID else { return }
                lastScrolledID = id
                withAnimation(.easeInOut(duration: 0.3)) {
                    proxy.scrollTo(id, anchor: .top)
                }
            }
            .onAppear {
                // Scroll to initial selection on first load
                if let id = selectedSectionID {
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
                        proxy.scrollTo(id, anchor: .top)
                    }
                }
            }
        }
    }
}

When you also need keyboard focus on the target section (e.g. to auto-focus a text field), combine scrollTo with @FocusState:

@FocusState private var focusedField: FieldAnchor?
enum FieldAnchor: Hashable { case section(UUID) }

// In onChange:
.onChange(of: selectedSectionID) { _, newValue in
    guard let id = newValue else { return }
    withAnimation {
        proxy.scrollTo(id, anchor: .top)
    }
    // Delay focus to let scroll settle
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.35) {
        focusedField = .section(id)
    }
}

Verification

  1. Each ForEach child has .id(section.id) — the id is stable across renames and not derived from \.self or array index.
  2. scrollTo is called inside .onChange(of: selectedSectionID) — NOT in the ForEach body, NOT in .task, NOT during view init.
  3. Uses VStack (not LazyVStack) for reliable scroll offset — or verify LazyVStack scroll accuracy on your platform if you need lazy rendering.
  4. lastScrolledID guard prevents re-scrolling to the same section on unrelated state changes.
  5. On macOS HSplitView: click a row in the sidebar → detail pane scrolls to that section's header within 0.5s.

Source

View on GitHub →

Search

Find related lessons →

← Back to MisakaNet · Search lessons