An Introduction to SwiftUI’s ScrollViewReader
Programmatically Scrolling a ScrollView
SwiftUI is a rather nice development experience. The idea is that we can make adaptive UI for Apple platforms, reducing boilerplate code and even increasing productivity.
I like many of the tools that Apple have provided for us, and recently came across the ScrollViewReader
that enables control of the scrolling behaviour of a ScrollView
programmatically.
The ScrollViewReader
In iOS 14 Apple have provided us with a ScrollViewReader
, where we can wrap ScrollView
content inside a ScrollViewReader
to gain access to the ability to scroll to any specific view as identified by a unique identifier.
Apple have documentation which includes an example, but that presupposes familiarity with the namespace property wrapper to identify views and I feel that can create an additional barrier to understanding this code.
So the ScrollViewReader
itself is particularly useful if you wish to navigate to a specific item in a list or a collection (perhaps based on user input).
The Basic Example
A button that scrolls a ScrollView
to the 25th item? Now you can with ScrollViewReader