Block scroll down in ScrollView - SwiftUI

前端 未结 1 402
梦如初夏
梦如初夏 2020-12-12 08:07

How can I block scroll down and only allow scroll up in order to avoid seeing the white space over the rectangle on top when scrolling?

struct ContentView: Vi         


        
相关标签:
1条回答
  • 2020-12-12 08:41

    I assume you want to avoid bounces, here is possible approach (tested with Xcode 12 / iOS 14)

    struct ContentView: View {
    
        var body: some View {
            GeometryReader { geo in
                ScrollView {
                    Rectangle()
                    .frame(width: geo.size.width, height: 1800)
                    .foregroundColor(.black)
                    .background(ScrollViewConfigurator {
                        $0?.bounces = false               // << here !!
                    })
                    Spacer()
                }
            }
        }
    }
    
    struct ScrollViewConfigurator: UIViewRepresentable {
        let configure: (UIScrollView?) -> ()
        func makeUIView(context: Context) -> UIView {
            let view = UIView()
            DispatchQueue.main.async {
                configure(view.enclosingScrollView())
            }
            return view
        }
    
        func updateUIView(_ uiView: UIView, context: Context) {}
    }
    
    

    Note: enclosingScrollView() helper is taken from my answer in How to scroll List programmatically in SwiftUI?

    0 讨论(0)
提交回复
热议问题