在 SwiftUI 列表/表单中禁用滚动 [英] Disable Scrolling in SwiftUI List/Form

查看:41
本文介绍了在 SwiftUI 列表/表单中禁用滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直致力于创建一个复杂的视图,该视图允许我在表单下方使用选择器.在每种情况下,表单都只有两个选项,因此没有足够的数据向下滚动以获取更多数据.能够滚动这个表单而不是下面的 Picker 会让视图感觉很糟糕.我无法将选择器放置在表单内,否则 SwiftUI 会更改选择器上的样式.而且我找不到任何地方是否可以在不使用的情况下禁用列表/表单上的滚动:

Lately, I have been working on creating a complex view that allows me to use a Picker below a Form. In every case, the Form will only have two options, thus not enough data to scroll downwards for more data. Being able to scroll this form but not Picker below makes the view feel bad. I can't place the picker inside of the form or else SwiftUI changes the styling on the Picker. And I can't find anywhere whether it is possible to disable scrolling on a List/Form without using:

.disable(condition)

有没有办法在不使用上述语句的情况下禁用列表或表单上的滚动?这是我的代码供参考

Is there any way to disable scrolling on a List or Form without using the above statement? Here is my code for reference

VStack{
        Form {
            Section{
                Toggle(isOn: $uNotifs.notificationsEnabled) {
                    Text("Notifications")
                }
            }
            if(uNotifs.notificationsEnabled){
                Section {
                    Toggle(isOn: $uNotifs.smartNotifications) {
                        Text("Enable Smart Notifications")
                    }
                }.animation(.easeInOut)
            }
       } // End Form
            .listStyle(GroupedListStyle())
            .environment(\.horizontalSizeClass, .regular)
        if(!uNotifs.smartNotifications){
                GeometryReader{geometry in
                    HStack{
                        Picker("",selection: self.$hours){
                            ForEach(0..<24){
                                Text("\($0)").tag($0)
                            }

                        }
                            .pickerStyle(WheelPickerStyle())
                            .frame(width:geometry.size.width / CGFloat(5))
                            .clipped()
                        Text("hours")
                        Picker("",selection: self.$min){
                            ForEach(0..<61){
                                Text("\($0)").tag($0)
                            }

                        }
                            .pickerStyle(WheelPickerStyle())
                            .frame(width:geometry.size.width / CGFloat(5))
                            .clipped()
                        Text("min")
                    }

推荐答案

在这里

使用我帖子中的方法SwiftUI:如何以编程方式滚动列表[解决方案]?,可以添加以下扩展

Using approach from my post SwiftUI: How to scroll List programmatically [solution]?, it is possible to add the following extension

extension ListScrollingProxy {
    func disableScrolling(_ flag: Bool) {
        scrollView?.isScrollEnabled = !flag
    }
}

并在上面的演示示例中使用它

and the use it as in example for above demo

struct DemoDisablingScrolling: View {
    private let scrollingProxy = ListScrollingProxy()

    @State var scrollingDisabled = false
    var body: some View {
        VStack {
            Button("Scrolling \(scrollingDisabled ? "Off" : "On")") {
                self.scrollingDisabled.toggle()
                self.scrollingProxy.disableScrolling(self.scrollingDisabled)
            }
            Divider()
            List(0..<50, id: \.self) { i in
                Text("Item \(i)")
                    .background(ListScrollingHelper(proxy: self.scrollingProxy))
            }
        }

    }
}

这篇关于在 SwiftUI 列表/表单中禁用滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆