Search code examples
iosswiftuiscrollviewuicollectionview

Add Multiple UICollectionView in UIScrollView with Paging by using single ViewController


I would like to have a paging UIScrollView with five UICollectionView that each have different data. Is it possible with one ViewController?


Solution

  • Yes Possible.

    You have to create 5 UICollectionView inside UIScrollView's ContentView [ContentView means, you have to create UIView inside UIScrollView with same UIScrollView's frame. Then, each UICollectionView should be inside within that ContentView] having width as UIScrollView's Width

    Each X position of UICollectionView should have difference as UIScrollView's Width

    Suppose, your UIScrollView frame is: {0,0,375,500}, 1st CollectionView Frame: = {0,0,375,500} 2nd CollectionView Frame: = {375,0,375,500} 3rd CollectionView Frame: = {750,0,375,500} and goes on...

    Then, you have to enable paging in UIScrollView.

    Then, you have to give UIScrollView's content size as, width = {number_of_collectionviews * UICollectionView's width} and height = 500

    Thats it.