Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding a UICollectionView in a UIScrollView works almost perfectly

Background

I embedded a UICollectionView inside of a UIScrollView and it works almost perfectly.

Goal

The scrollable elements should pass off the scroll seamlessly once they've reached the end of their contentViews. When I swipe down on the UIScrollView it should scrolls down so that the UICollectionView takes up ~90% of the screen and then continues scrolling the collectionView. The reverse should be true as well, when I scroll up on the collectionView and reach the top of the collectionView content the scrollview should start scrolling up.

Problem

1) When I scroll up on the collectionView, it scrolls to the top and stops. If I flick down a couple times it still bounces the collectionView instead of scrolling the scrollView up. I have to tap the collectionView one more time "deliberately" in order to get the parent ScrollView to scroll up.

See gif:
double bounce

I imagine it has something to do with CancelContentTouches but I haven't been able to get the correct combination.

2) Same thing when I scroll down, once the scrollview reaches the end of the contentView, it stops and bounces.

See gif:
Scroll down and stick

Note: I realize it's not super recommended to put a UICollectionView in a UIScrollView. I tried alternatives listed below, but I wasn't able to achieve the desired effect by putting my top view in the header. I would like to be able to scroll both independently, so I decided to go with scrollview/collectionView combo.

View Hierarchy

MainViewController
- ScrollView
-- Profile Stats
-- CollectionViewContainerView
--- CollectionView

Resources other people looking to put collectionViews into scrollViews for some reason:
I tried them, but they didn't work for my use case.

  • https://www.raywenderlich.com/156794/custom-uicollectionviewlayout-tutorial-parallax
  • https://www.youtube.com/watch?v=ePCliV2CsuU&index=8&list=PL23Revp-82LKxKN9SXqQ5Nxaa1ZpYEQua
like image 298
Emin Israfil iOS Avatar asked Jan 13 '18 05:01

Emin Israfil iOS


1 Answers

If you are using scrollview or subclass of it in another scrollview or subclass of it then it is happen.

Now, you can do one thing. Disable scrolling for one (Inner scrollview recommended) and implement scrollview's delegate like scrollViewDidScroll or other which is more suitable and scroll the inner scrollview manually (by setting content offset of inner scrollview).

like image 73
Ketan Parmar Avatar answered Nov 17 '22 04:11

Ketan Parmar