9

I have list with items. How can i scroll to list 12. I can use geometry reader to calculate offset. But how to scroll to this offset?

List {
      ForEach(0..<12) { index in
          Text("...")          
      }  
}
Tim
  • 1,798
  • 2
  • 15
  • 20
  • 1
    If your app design allows to use ScrollView instead of List, you can consider approach demonstrated in [How to make a SwiftUI List scroll automatically?](https://stackoverflow.com/questions/57258846/how-to-make-a-swiftui-list-scroll-automatically/58708206#58708206) post. – Asperi Nov 22 '19 at 17:30

1 Answers1

6

Form Xcode 12, You can turn in to a ScrollView and then you can do it by .scrollTo(id):

var body: some View {
    ScrollViewReader { scrollProxy in
        ScrollView {
            ForEach((1...100), id: \.self) { Text("\($0)") }
        }
        Button("Go!") {
            withAnimation { scrollProxy.scrollTo(50) }
        }
    }
}

Note that ScrollViewReader should support all scrollable content, but now it only supports ScrollView

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278