Scrolling programmatically with ScrollViewReader
ScrollViewReader
allows you to programmatically scroll to a different index in a list that might or might not be 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, we will create an app that displays a list of characters from A to Q. A button at the top of the screen will programmatically scroll from the top to the end of the list when clicked. Another button at the bottom of the screen will allow us to scroll back up from the bottom to the middle of the list.
Getting ready
Create a new SwiftUI project named ScrollViewReaders
.
How to do it…
We'll start by creating an Identifiable
struct that has two properties: a name
and an id
.
Then, we'll use this struct
to create an array of SF Symbols to be displayed on the screen. Finally, we...