Search code examples
iosswiftiphoneuiscrollviewscrollview

UIScrollView disable top bounce


I have a ViewController that contains an UIScrollView and I'm trying to disable the bounce effect only on the top of the view.

I tried to add this answer in my ViewController class, which is saying to do this :

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    //disable bounce only at the top of the screen
    scrollView.bounces = scrollView.contentOffset.y > 100
}

But I don't really understand what to do with this. I also tried to implement the UIScrollViewDelegate.

Nothing I tried worked.

Here is my StoryBoard and ViewController

What do I need to do in order to make it works ?

My setup :

  • Swift version : 5.1.3
  • Xcode version : 11.3.1
  • Target iOS version : 13.2
  • MacBook Pro (13-inch, 2016, Four Thunderbolt 3 Ports) MacOS Catalina 10.15.2 (19C57)

Solution

  • Connect scrollView outlet and then try adding scrollView.delegate = self or add scrollView.bounces = scrollView.contentOffset.y > 100 in your viewDidLoad() function.