Disable Scrolling in SwiftUI List/Form

瘦欲@ 提交于 2020-08-07 09:42:24

问题


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")
                    }

回答1:


Here it is

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

    }
}


来源:https://stackoverflow.com/questions/60905671/disable-scrolling-in-swiftui-list-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!