rmluux
rmluux

Reputation: 43

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 :

Upvotes: 2

Views: 2035

Answers (5)

Ucdemir
Ucdemir

Reputation: 3096

Go to Attributes Inspector. uncheck "Bounce On Scrolll"

Upvotes: 0

rmluux
rmluux

Reputation: 43

Okay here is the answer to my problem :

  • In the viewController, add the ScrollView outlet
  • In the viewDidLoad() function, add this myScrollView.delegate = self
  • Make your viewController implement UIScrollViewDelegate : class MyViewController: UIViewController, UIScrollViewDelegate { ...
  • Then add the following function :

    func scrollViewDidScroll(scrollView: UIScrollView) { myScrollView.bounces = myScrollView.contentOffset.y > 100 }

Upvotes: 0

Valentina Konatar
Valentina Konatar

Reputation: 277

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

Upvotes: 4

Chetan Hedamba
Chetan Hedamba

Reputation: 291

try this one

func scrollViewDidScroll(scrollView: UIScrollView) {
      if scrollView.contentOffset.y < 0 {
          scrollView.contentOffset.y = 0
      }
  }

Upvotes: 0

Ketan Parmar
Ketan Parmar

Reputation: 27448

If you want to disable bounces of the scrollview then you can take outlet of your scrollview and can set bounces property to false in viewdidload like below,

yourScrollView.bounces = false

or you can uncheck bounces from the storyboard also!

Upvotes: 3

Related Questions