如何在 SwiftUI 中以编程方式滚动列表?

Asp*_*eri 32 ios swift swiftui

看起来在当前的工具/系统中,刚刚发布的 Xcode 11.4/iOS 13.4 中将没有 SwiftUI 原生支持“滚动到”功能List。因此,即使他们,Apple,将在下一个主要版本中提供它,我也需要对 iOS 13.x 的向后支持。

那么我将如何以最简单和轻松的方式做到这一点?

  • 滚动列表结束
  • 滚动列表到顶部
  • 和别的

(我不喜欢像之前在 SO 上提出的那样将完整的UITableView基础设施包装起来UIViewRepresentable/UIViewControllerRepresentable)。

Asp*_*eri 49

SWIFTUI 2.0

这是 Xcode 12 / iOS 14 (SwiftUI 2.0) 中可能的替代解决方案,当滚动控件位于滚动区域之外时,可以在同一场景中使用(因为 SwiftUI2ScrollViewReader只能在内部使用 ScrollView

注意:行内容设计不在考虑范围内

使用 Xcode 12b / iOS 14 测试

演示2

class ScrollToModel: ObservableObject {
    enum Action {
        case end
        case top
    }
    @Published var direction: Action? = nil
}

struct ContentView: View {
    @StateObject var vm = ScrollToModel()

    let items = (0..<200).map { $0 }
    var body: some View {
        VStack {
            HStack {
                Button(action: { vm.direction = .top }) { // < here
                    Image(systemName: "arrow.up.to.line")
                      .padding(.horizontal)
                }
                Button(action: { vm.direction = .end }) { // << here
                    Image(systemName: "arrow.down.to.line")
                      .padding(.horizontal)
                }
            }
            Divider()
            
            ScrollView {
                ScrollViewReader { sp in
                    LazyVStack {
                        ForEach(items, id: \.self) { item in
                            VStack(alignment: .leading) {
                                Text("Item \(item)").id(item)
                                Divider()
                            }.frame(maxWidth: .infinity).padding(.horizontal)
                        }
                    }.onReceive(vm.$direction) { action in
                        guard !items.isEmpty else { return }
                        withAnimation {
                            switch action {
                                case .top:
                                    sp.scrollTo(items.first!, anchor: .top)
                                case .end:
                                    sp.scrollTo(items.last!, anchor: .bottom)
                                default:
                                    return
                            }
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

SWIFTUI 1.0+

这是一种有效的方法的简化变体,看起来很合适,并且需要几个屏幕代码。

使用 Xcode 11.2+ / iOS 13.2+ 测试(也使用 Xcode 12b / iOS 14)

使用演示:

struct ContentView: View {
    private let scrollingProxy = ListScrollingProxy() // proxy helper

    var body: some View {
        VStack {
            HStack {
                Button(action: { self.scrollingProxy.scrollTo(.top) }) { // < here
                    Image(systemName: "arrow.up.to.line")
                      .padding(.horizontal)
                }
                Button(action: { self.scrollingProxy.scrollTo(.end) }) { // << here
                    Image(systemName: "arrow.down.to.line")
                      .padding(.horizontal)
                }
            }
            Divider()
            List {
                ForEach(0 ..< 200) { i in
                    Text("Item \(i)")
                        .background(
                           ListScrollingHelper(proxy: self.scrollingProxy) // injection
                        )
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

演示

解决方案:

注入List可表示的轻视图可以访问 UIKit 的视图层次结构。当List重用行时,没有更多的值然后将行放入屏幕。

struct ListScrollingHelper: UIViewRepresentable {
    let proxy: ListScrollingProxy // reference type

    func makeUIView(context: Context) -> UIView {
        return UIView() // managed by SwiftUI, no overloads
    }

    func updateUIView(_ uiView: UIView, context: Context) {
        proxy.catchScrollView(for: uiView) // here UIView is in view hierarchy
    }
}
Run Code Online (Sandbox Code Playgroud)

找到封闭UIScrollView(需要做一次)然后将所需的“滚动到”操作重定向到存储的滚动视图的简单代理

class ListScrollingProxy {
    enum Action {
        case end
        case top
        case point(point: CGPoint)     // << bonus !!
    }

    private var scrollView: UIScrollView?

    func catchScrollView(for view: UIView) {
        if nil == scrollView {
            scrollView = view.enclosingScrollView()
        }
    }

    func scrollTo(_ action: Action) {
        if let scroller = scrollView {
            var rect = CGRect(origin: .zero, size: CGSize(width: 1, height: 1))
            switch action {
                case .end:
                    rect.origin.y = scroller.contentSize.height +
                        scroller.contentInset.bottom + scroller.contentInset.top - 1
                case .point(let point):
                    rect.origin.y = point.y
                default: {
                    // default goes to top
                }()
            }
            scroller.scrollRectToVisible(rect, animated: true)
        }
    }
}

extension UIView {
    func enclosingScrollView() -> UIScrollView? {
        var next: UIView? = self
        repeat {
            next = next?.superview
            if let scrollview = next as? UIScrollView {
                return scrollview
            }
        } while next != nil
        return nil
    }
}
Run Code Online (Sandbox Code Playgroud)


Lac*_*rov 14

首选方式

这个答案越来越受到关注,但我应该声明这ScrollViewReader是正确的方法。仅当阅读器/代理由于版本限制而不适用于您时,才会采用内省方式。

ScrollViewReader { proxy in
    ScrollView(.vertical) {
        TopView().id("TopConstant")
        ...
        MiddleView().id("MiddleConstant")
        ...
        Button("Go to top") {
            proxy.scrollTo("TopConstant", anchor: .top)
        }
        .id("BottomConstant")
    }
    .onAppear{
        proxy.scrollTo("MiddleConstant")
    }
    .onChange(of: viewModel.someProperty) { _ in
        proxy.scrollTo("BottomConstant")
    }
}
Run Code Online (Sandbox Code Playgroud)

字符串应定义在 body 属性之外的一处。

旧版答案

这是一个适用于 iOS13&14 的简单解决方案:
使用Introspect
我的情况是初始滚动位置。

ScrollView(.vertical, showsIndicators: false, content: {
        ...
    })
    .introspectScrollView(customize: { scrollView in
        scrollView.scrollRectToVisible(CGRect(x: 0, y: offset, width: 100, height: 300), animated: false)
    })
Run Code Online (Sandbox Code Playgroud)

如果需要,可以根据屏幕尺寸或元素本身计算高度。该解决方案适用于垂直滚动。对于水平方向,您应该指定 x 并将 y 保留为 0

  • 这是目标 SwiftUI 1.0 的最佳答案 (3认同)

Moj*_*ini 10

只需滚动到 id:

scrollView.scrollTo(ROW-ID)
Run Code Online (Sandbox Code Playgroud)

由于 SwiftUI 结构化设计的数据驱动,你应该知道你所有的项目 ID。所以,你可以滚动到任何ID与ScrollViewReader来自iOS的14,并用Xcode的12

struct ContentView: View {
    let items = (1...100)

    var body: some View {
        ScrollViewReader { scrollProxy in
            ScrollView {
                ForEach(items, id: \.self) { Text("\($0)"); Divider() }
            }

            HStack {
                Button("First!") { withAnimation { scrollProxy.scrollTo(items.first!) } }
                Button("Any!") { withAnimation { scrollProxy.scrollTo(50) } }
                Button("Last!") { withAnimation { scrollProxy.scrollTo(items.last!) } }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意 ScrollViewReader应该支持所有可滚动的内容,但现在只支持ScrollView


预览

预览


ing*_*nti 8

我的两分钱用于根据其他逻辑随时删除和重新定位列表..即在删除/更新之后,例如转到顶部。(这是一个超简化的示例,我在网络回调重新定位后使用了此代码:在网络调用后我更改 previousIndex )

结构ContentView:视图{

@State private var previousIndex : Int? = nil
@State private var items = Array(0...100)

func removeRows(at offsets: IndexSet) {
    items.remove(atOffsets: offsets)
    self.previousIndex = offsets.first
}

var body: some View {
    ScrollViewReader { (proxy: ScrollViewProxy) in
        List{
            ForEach(items, id: \.self) { Text("\($0)")
            }.onDelete(perform: removeRows)
        }.onChange(of: previousIndex) { (e: Equatable) in
            proxy.scrollTo(previousIndex!-4, anchor: .top)
            //proxy.scrollTo(0, anchor: .top) // will display 1st cell
        }

    }
    
}
Run Code Online (Sandbox Code Playgroud)

}

  • 我只想指出,这是唯一的解决方案,它使用列表,就像问题的标题一样。在 iOS 15.1 上测试并有效。谢谢。 (4认同)
  • 谢谢汤姆!在 iOS 16 上进行测试 (2认同)

小智 7

谢谢 Asperi,很棒的提示。当在视图之外添加新条目时,我需要向上滚动列表。重新设计以适应 macOS。

我将状态/代理变量带到环境对象并在视图外使用它来强制滚动。我发现我必须更新它两次,第二次有 0.5 秒的延迟才能获得最佳结果。第一次更新可防止视图在添加行时滚动回顶部。第二次更新滚动到最后一行。我是新手,这是我的第一个 stackoverflow 帖子:o

为 MacOS 更新:

struct ListScrollingHelper: NSViewRepresentable {

    let proxy: ListScrollingProxy // reference type

    func makeNSView(context: Context) -> NSView {
        return NSView() // managed by SwiftUI, no overloads
    }

    func updateNSView(_ nsView: NSView, context: Context) {
        proxy.catchScrollView(for: nsView) // here NSView is in view hierarchy
    }
}

class ListScrollingProxy {
    //updated for mac osx
    enum Action {
        case end
        case top
        case point(point: CGPoint)     // << bonus !!
    }

    private var scrollView: NSScrollView?

    func catchScrollView(for view: NSView) {
        //if nil == scrollView { //unB - seems to lose original view when list is emptied
            scrollView = view.enclosingScrollView()
        //}
    }

    func scrollTo(_ action: Action) {
        if let scroller = scrollView {
            var rect = CGRect(origin: .zero, size: CGSize(width: 1, height: 1))
            switch action {
                case .end:
                    rect.origin.y = scroller.contentView.frame.minY
                    if let documentHeight = scroller.documentView?.frame.height {
                        rect.origin.y = documentHeight - scroller.contentSize.height
                    }
                case .point(let point):
                    rect.origin.y = point.y
                default: {
                    // default goes to top
                }()
            }
            //tried animations without success :(
            scroller.contentView.scroll(to: NSPoint(x: rect.minX, y: rect.minY))
            scroller.reflectScrolledClipView(scroller.contentView)
        }
    }
}
extension NSView {
    func enclosingScrollView() -> NSScrollView? {
        var next: NSView? = self
        repeat {
            next = next?.superview
            if let scrollview = next as? NSScrollView {
                return scrollview
            }
        } while next != nil
        return nil
    }
}
Run Code Online (Sandbox Code Playgroud)