One way to disable bounces for specific ScrollView
is to access the underlying UIScrollView
from a SwiftUI hierarchy.
check this link, and this link
struct UIKitPeeker: UIViewRepresentable {
let callback: (UIScrollView?) -> Void
func makeUIView(context: Context) -> MyView {
let view = MyView()
view.callback = callback
return view
}
func updateUIView(_ view: MyView, context: Context) { }
class MyView: UIView {
var callback: (UIScrollView?) -> () = { _ in }
override func didMoveToWindow() {
super.didMoveToWindow()
var targetView: UIView? = self
while if let currentView = targetView {
if currentView is UIScrollView {
break
} else {
currentView = currentView.superView
}
}
callback(targetView as? UIScrollView)
}
}
}
To use it:
struct ContentView: View {
var body: some View {
ScrollView {
VStack(spacing: 8) {
Foreach(0..<100, id: \.self) { i in
Text("Scroll bounces disabled \(i)")
}
}
.background {
UIKitPeeker { scrollView in
scrollView?.bounces = false
}
}
}
}
}