Manisha
Manisha

Reputation: 181

Remove Collection view bounce issue when using pull to refresh

I have one collection view configured as follow

        superCollectionView!.alwaysBounceHorizontal = false
        superCollectionView!.alwaysBounceVertical = false

        if #available(iOS 10.0, *) {
            superCollectionView!.refreshControl = refreshControl
        } else {
            superCollectionView!.backgroundView = refreshControl
        }

but bounce effect is still there. I want to remove bounce from bottom...

Upvotes: 1

Views: 1862

Answers (1)

Ahmad F
Ahmad F

Reputation: 31665

If you want to remove bouncing only from the bottom (For letting the refreshControl to be available), I'd suggest to handle it in scroll​View​Did​Scroll:​ method to check if the scroll view contentOffset.y has been reached to bottom of the scroll view (logically, it is the content size of the scroll view minus the height of the visible frame of the scroll view), as follows:

Solution:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height {
        scrollView.setContentOffset(CGPoint(x: scrollView.contentOffset.x, y: scrollView.contentSize.height - scrollView.frame.size.height), animated: false)
    }
}

Output:

After implementing scroll​View​Did​Scroll as mentioned above, it should be behaves like:

enter image description here

Also:

What about achieving the opposite?

Referring to the above description, preventing the top bouncing would be:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y < 0 {
        scrollView.setContentOffset(CGPoint(x: scrollView.contentOffset.x, y: 0), animated: false)
    }
}

Upvotes: 1

Related Questions