Scrolling programmatically
In iOS 17, ScrollView
received a series of improvements. One of these is the scrollPosition(id:)
modifier, which associates a binding to be updated when the scroll view scrolls. The binding allows us to read the ID of the topmost or leading-most view shown in the scroll view at any given moment. We can also set the binding to the ID of our choice and the scroll view will scroll programmatically to show the chosen view.
If we are targeting previous versions of iOS, we must use ScrollViewReader
to read the scroll position and to scroll programmatically. ScrollViewReader
allows us to programmatically scroll to a different view in the scroll view, even if the view is not currently visible on the screen. For example, ScrollViewReader
could be used to programmatically scroll to a newly added item, scroll to the most recently changed item, or scroll based on a custom trigger.
In this recipe, to showcase how to scroll programmatically, we will create an...