【问题标题】:scrollToRow(at:at:animated:) SwiftUIscrollToRow(at:at:animated:) SwiftUI
【发布时间】:2019-11-24 06:08:11
【问题描述】:

我有 List 有 100 个项目 我想滚动到项目编号 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) {

    }
}

【问题讨论】:

    标签: ios swiftui


    【解决方案1】:

    早些时候试图为这个answering here 找到一些解决方案。不久:今天(2019 年 11 月)我没有找到解决方案,除了使用 ScrollViewUITableView(最后一个 UIViewControllerRepresentable

    更新在 9 月发现 Apple Developer Forums thread 有这个问题,但仍然没有答案

    【讨论】:

      【解决方案2】:

      使用 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)
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-18
        • 1970-01-01
        相关资源
        最近更新 更多