2

I understand for iOS 14 you can use ScrollViewReader to automatically scroll your view up or down as the content in the scrollview changes.

I'm in a situation where I have to support iOS 13. Is there a way, even a hacky way, to programmatically scroll the scrollview in iOS 13?

tentmaking
  • 2,076
  • 4
  • 30
  • 53

1 Answers1

1

To do this on iOS 13 requires you to create a SwiftUI wrapper for a UIScrollView, and use the UIKit methods to achieve this.

Luckily there are several open source packages that can make this much easier for you. One of them is https://github.com/Amzd/ScrollViewProxy. I've used this package with good results in the past.

Example:

ScrollView { proxy in
  VStack {
    Text("A").scrollId("A")
    Text("B").scrollId("B")
    Text("C").scrollId("C")

    Button("Scroll to C") {
      proxy.scrollTo("C", alignment: .center)
    }

    Button("Scroll down") {
      proxy.scrollTo(.bottom)
    }
  }
}
Rengers
  • 14,911
  • 1
  • 36
  • 54