Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI List - How to scroll to item

Tags:

swiftui

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("...")          
      }  
}
like image 597
Tim Avatar asked Nov 22 '19 14:11

Tim


1 Answers

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

like image 67
Mojtaba Hosseini Avatar answered Sep 28 '22 09:09

Mojtaba Hosseini