在 ScrollView 中阻止向下滚动 - SwiftUI [英] Block scroll down in ScrollView - SwiftUI
本文介绍了在 ScrollView 中阻止向下滚动 - SwiftUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何阻止向下滚动而只允许向上滚动以避免在滚动时看到顶部矩形上方的空白?
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()
}
}
}
}
推荐答案
我假设你想避免反弹,这里是可能的方法(使用 Xcode 12/iOS 14 测试)
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) {}
}
注意:enclosureScrollView()
帮助器取自我在 How to scroll List programmatically 中的回答在 SwiftUI 中?
Note: enclosingScrollView()
helper is taken from my answer in How to scroll List programmatically in SwiftUI?
这篇关于在 ScrollView 中阻止向下滚动 - SwiftUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文