Disable Scrolling in SwiftUI List/Form
Asked Answered
N

2

6

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")
                    }
Nigritude answered 28/3, 2020 at 19:9 Comment(0)
S
6

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

Shrew answered 19/12, 2022 at 20:27 Comment(1)
Yes, this works. So I upvoted... But not for my case: I have a Form which contains a TextEditor. Putting this on the Form disables scrolling in the TextEditor as well...Commercialism
T
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))
            }
        }

    }
}
Tonneson answered 29/3, 2020 at 5:43 Comment(1)
I've tried your code for Form. But it had no effect. Does this solution works for SwiftUI/Form too?Polysynthetic

© 2022 - 2024 — McMap. All rights reserved.