0

I know this has been solved in iOS 14 but my current project is in iOS 13 and we have to release it fast. I tried multiple approach from stack but no luck. I just want to click a button and scroll to the bottom of the ScrollView, thats it. I was surprised there is not built in function in SwiftUI for this !!!

struct ContentView: View {
var items = [Color.red, Color.orange, Color.yellow, Color.green,Color.blue, Color.purple, Color.red, Color.orange, Color.yellow, Color.green,Color.blue, Color.purple]

var body: some View {
    ZStack {
        ScrollView(Axis.Set.vertical, showsIndicators: false) {
            VStack {
                ForEach(self.items, id: \.self) { item in
                    // 3.
                    Circle()
                        .fill(item)
                        .frame(width: 100, height: 100)
                }
            }
        }
        
        VStack {
            Spacer()
            HStack {
                Spacer()
                Button(action: {
                    // scroll to bottom
                }) {
                    Text("Button")
                }
            }
            .padding(.trailing, 20)
            .padding(.bottom, 20)
            
        }
        
    }
    
}
}

If anybody could help

Arafin Russell
  • 1,487
  • 1
  • 18
  • 37
  • 1
    Consider approach in https://stackoverflow.com/a/60855853/12299030, `ListScrollingProxy` works for `ScrollView` as well. Or this topic for custom scroll view https://stackoverflow.com/a/58708206/12299030 – Asperi Aug 19 '20 at 18:29
  • I already tried one of your solution, I will try another. Thank you – Arafin Russell Aug 19 '20 at 18:44

1 Answers1

2

After a few hours search i found this amazing library that does exactly as you want

ScrollView{ proxy in
            Button("click"){
                proxy.scrollTo(200)
            }.foregroundColor(.red)
            ForEach(0..<1000){ index in
                Text("\(index)")
                    .scrollId(index)
            }
        }