5

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")
                    }
Parth
  • 2,682
  • 1
  • 20
  • 39
Jacob Miller
  • 51
  • 1
  • 5

2 Answers2

5

Here it is

demo

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

    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
3

You can use the .scrollDisabled(true) modifier on the component (Form or List) to accomplish this behavior.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
DyN
  • 31
  • 1