10

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?

test
  • 373
  • 1
  • 3
  • 10

3 Answers3

5

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.

Anshuman Singh
  • 1,018
  • 15
  • 17
  • Can we use UIScrollView().alwaysBounceVertical = false ?? – Nabeel Nazir May 14 '20 at 05:31
  • @NabeelNazir I guess this won't work, the way you are suggesting is basically for an instance of the UIScrollView , so for that, you must have an instance, and that won't be possible with SwiftUI. So applying it on the appearance of it will eventually update or override the underlying appearance of the UIScrollView class, so it will be applicable throughout the application. I have explained it in detail with this answer:-https://stackoverflow.com/a/59897811/6445871 , have a look at it. – Anshuman Singh May 14 '20 at 12:59
  • I've read from somewhere that appearance() doesn't have bounces property anymore, is it true ? If yes, then what will be the other way to disable the bounce of scrollview. – Nabeel Nazir May 15 '20 at 04:29
  • 1
    @NabeelNazir I have gone through the UIScrollView documentation, and all the properties mentioned in the base class can be applied on the appearance(). I have implemented the same thing in my app, and it works fine for me. But if you want to disable it on a particular screen and then you would have to enable it in the view onDisappear. – Anshuman Singh May 15 '20 at 12:02
  • The problem I have with this solution is that without bouncing I can't have large navigation titles. But with the bouncing there is a lot of flickering. – PoolHallJunkie May 21 '21 at 18:15
0

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)
kontiki
  • 37,663
  • 13
  • 111
  • 125
  • 3
    Sadly, thats only for the axis direction of the ScrollView. I need to disable the bounce when a user scroll fast. In UIKit there is a scrollView.bounce = false, and in previous beta there was a alwaysBounceVertical. But the sdk changed and I can't seem to disable it anymore – test Jul 21 '19 at 12:35
  • I'm afraid ScrollView is very limited at the moment. Lots of things you cannot do with it. – kontiki Jul 21 '19 at 12:38
  • Aah what a bummer! I do think so too! At least thanks for your time! – test Jul 21 '19 at 14:01
0

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 { [...]