Skip to content

ScrollView Scroll Offset (SwiftUI)

A SwiftUI guide for coding agents. Also covers scrollposition type, programmatic scroll to offset, scroll to top bottom button, scrollto id anchor, animate scroll position.

The ScrollPosition type (iOS 18) drives a ScrollView programmatically by edge, view id, or exact point, through one binding. It supersedes id-only tracking with scrollPosition(id:) when you need more than "which view is visible".

Binding the Position

Bind the state with scrollPosition and call scrollTo on it. Attaching animation with the position as its value animates every programmatic scroll.

@State private var position = ScrollPosition(edge: .top)

ScrollView {
  Button("Scroll to bottom") {
    position.scrollTo(edge: .bottom)
  }

  ForEach(1..<100) { index in
    Text(verbatim: index.formatted())
      .id(index)
  }
}
.scrollPosition($position)
.animation(.default, value: position)

The scrollTo Overloads

  • scrollTo(edge:) jumps to .top or .bottom.
  • scrollTo(id:anchor:) targets a view id; anchor picks which point of that view lands visible, such as .center.
  • scrollTo(point:) takes a CGPoint offset into the content.
  • scrollTo(x:) and scrollTo(y:) move one axis and leave the other alone.
position.scrollTo(id: 42, anchor: .center)
position.scrollTo(point: CGPoint(x: 0, y: 100))
position.scrollTo(y: 100)

Reading It Back, and the Gotcha

The position exposes optional edge, point, and viewID values, but only for programmatic scrolls. As soon as the user drags, those properties become nil; isPositionedByUser tells you that happened. To observe the live offset during a user gesture, use onScrollGeometryChange instead; ScrollPosition cannot do it.

Use this guidance in your coding agent

Install the Better Design MCP once. Your agent then loads this page with one call.

get-swiftui-guide({ topic: "swiftui-mastering-scrollview-scroll-offset" })
claude mcp add --scope user better-design --transport http https://better-design.com/api/mcp --header "Authorization: Bearer <YOUR_API_KEY>"
Browse related design systems