Tim
Tim

Reputation: 1928

SwiftUI List - How to scroll to item

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("...")          
      }  
}

Upvotes: 9

Views: 2902

Answers (1)

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119917

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

Upvotes: 6

Related Questions