Reputation: 383
There was a property in the initializer with ScrollView(alwaysBounceVertical: false) but I cant't seem to find it anymore.
Does anyone know how I can disable the vertical bounce / overscroll on a SwiftUI ScrollView?
Upvotes: 11
Views: 6582
Reputation: 33
Create a view extension that extracts the underlying UIKit view from SwiftUI, you can make modifications to the view from there, including disabling bounces.
extension View {
@ViewBuilder
func viewExtractor(result: @escaping (UIView) -> ()) -> some View {
self
.background(ViewExtractorHelper(result: result))
.compositingGroup()
}
}
fileprivate struct ViewExtractorHelper: UIViewRepresentable {
var result: (UIView) -> ()
func makeUIView(context: Context) -> UIView {
let view = UIView(frame: .zero)
view.backgroundColor = .clear
DispatchQueue.main.async {
if let superView = view.superview?.superview?.subviews.last?.subviews.first {
result(superView)
}
}
return view
}
func updateUIView(_ uiView: UIView, context: Context) {
}
}
Example:
ScrollView {
VStack(spacing: 1) {
ForEach(1 ... 50, id: \.self) { _ in
Rectangle()
.fill(.blue.mix(with: .black, by: 0.25))
.frame(height: 120)
}
}
.border(.yellow)
}
.viewExtractor { view in
if let scrollView = view as? UIScrollView {
scrollView.bounces = false
}
}
```
Upvotes: 1
Reputation: 326
You cannot control it properly with SwiftUI. Your two options are:
Create a UIScrollView
inside a UIViewRepresentable
like here (doesn't seem to work properly)
Or use some framework that allows you to access the UIScrollView
inside SwiftUI's ScrollView
.
Then in both cases you can set the bounce property of UIScrollView
to false.
Upvotes: 0
Reputation: 1
The only way I managed to make the bounce
disappear...
func setup()->Bool{
UIScrollView.appearance().bounces = false
return false
}
@main
struct myApp: App {
private var useless:Bool = setup()
var body: some Scene { [...]
Upvotes: 0
Reputation: 1126
Yes, it has been removed from the initializer of ScrollView, now you have to update the appearance of the UIScrollView in order to restrict the scrolling.
UIScrollView.appearance().bounces = false
This line will restrict the scrolling, you can apply this in the AppDelegate(didFinishLaunching) or the init() method of your View.
Upvotes: 5
Reputation: 40659
Yes, it changed in Beta 4:
ScrollView(.vertical, showsIndicators: false) { ... }
The new initializer is:
public init(_ axes: Axis.Set = .vertical, showIndicators: Bool = true, content: () -> Content)
Upvotes: 0