Controlling scrolling programmatically using ScrollViewReader
In this section, we will see how to make ScrollView
move to a specific position within its displayed content.
This result can be achieved by embedding ScrollViewReader
inside ScrollView
.
ScrollViewReader
has a scrollTo()
method, which can make the content of ScrollView
scroll to the position of the anchors of any of the views inside ScrollView
.
In the following code example, we will see how to scroll to the position of an arbitrary view inside ScrollView
:
import SwiftUI struct ContentView: View { let myColors: [Color] = [.red,.orange, .yellow,.green,.blue ] var body: some View { ScrollView { ScrollViewReader { value in Button("Scroll to end...