1

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: View {
    
    var body: some View {
        GeometryReader { geo in
            ScrollView {
                Rectangle()
                .frame(width: geo.size.width, height: 400)
                .foregroundColor(.black)
                Spacer()
            }
        }
    }
}
pawello2222
  • 34,912
  • 15
  • 99
  • 151
xmetal
  • 551
  • 4
  • 12

1 Answers1

7

Update: re-tested with Xcode 13.3 / iOS 15.4

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?

Test module in project is here

Asperi
  • 173,274
  • 14
  • 284
  • 455
  • 1
    I will try it soon. I just don't understand what should I put instead of enclosingScrollView – xmetal Aug 09 '20 at 23:36