我有包含100个项目的List,我想滚动到项目编号20,如何在SwiftUI中实现这一点
下面是我的简单ListCode
struct ContentView: View {
    var body: some View {
        List {
            Button(action: {
                self.scrollToIndex(index: 20)
            }) {
                Text("Scroll To 20")
            }
            ForEach(0..<100) {_ in
                Text("Hello World")
            }
        }
    }
    func scrollToIndex(index: Int) {
    }
}发布于 2019-11-24 16:02:33
早些时候,我试图为这个answering here找到一些解决方案。简要地说:今天(2019年11月),除了使用ScrollView或UITableView (制作最后一个UIViewControllerRepresentable)之外,我没有找到任何解决方案。
更新在9月份发现了Apple Developer Forums thread的这个问题,但仍然没有答案
发布于 2020-10-22 21:06:30
使用iOS 14中的新ScrollViewReader,您可以简单地执行以下操作:
struct ContentView: View {
    var body: some View {
        ScrollViewReader { proxy in
            List {
                Button(action: {
                    self.scrollToIndex(proxy, index: 20)
                }) {
                    Text("Scroll To 20")
                }
                ForEach(0..<100) { i in
                    Text("Hello World").id(i)
                }
            }
        }
    }
    func scrollToIndex(_ proxy: ScrollViewProxy, index: Int) {
        withAnimation {
            proxy.scrollTo(index)
        }
    }
}https://stackoverflow.com/questions/59015113
复制相似问题