Search code examples
iosuicollectionviewuicollectionviewlayout

UICollectionView - vertical scrollling, horizontal paging with custom layout


I'm trying to implement an iOS collection view which basically should work similar to a table view, but the cells' widths being integer multiples of the collection view's width (eg. 4 times the width). the collection view should scroll vertically (like a regular tableview does), but it should page horizontally across the content.

I do have the custom layout working except for the paging. Currently, the collectionView will just scroll horizontally. I'm unsure how to implement the paging correctly.

If I just set pagingEnabled on the collectionView, it has no effect. I suspect that the scrollview needs to be told what the width of a page is..?

Or do I have to do anything weird because there is only one cell/column per row, instead of multiple cells?

See the image; the grey bars are the cells, the blue rect is the collection view frame.

thanks!

enter image description here


Solution

  • I believe if the collection view item widths are less than or equal to the width of the collection view, then the horizontal paging should work via the pagingEnabled property. Otherwise, you need to create the paging effect yourself using the UIScrollView delegate methods and animating to offsets yourself after a certain threshold