如何通过单击按钮滚动到ScrollView的底部,SwiftUI iOS 13



我知道这个问题已经在iOS 14中解决了,但我目前的项目是在iOS 13中,我们必须尽快发布。我尝试了多种方法,但没有成功。我只想点击一个按钮,滚动到滚动视图的底部,就这样。我很惊讶SwiftUI中没有内置的功能!!!

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)

}

}

}
}

如果有人能帮助

经过几个小时的搜索,我发现了这个惊人的库,它完全符合您的要求

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

最新更新