Reputation: 43
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
Reputation: 43
Okay here is the answer to my problem :
viewDidLoad()
function, add this myScrollView.delegate = self
class MyViewController: UIViewController, UIScrollViewDelegate { ...
Then add the following function :
func scrollViewDidScroll(scrollView: UIScrollView) { myScrollView.bounces = myScrollView.contentOffset.y > 100 }
Upvotes: 0
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
Reputation: 291
try this one
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.y < 0 {
scrollView.contentOffset.y = 0
}
}
Upvotes: 0
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