Search code examples
iosuicollectionviewuicollectionviewlayout

Peek previous/next cells in UICollectionView with native paging enabled?


I want a collection view to page through cells and centered, but display a portion of the previous and next cells like this:

enter image description here

There are tons of hacks out there, but I'd like to achieve this with the native paging property of the UICollectionView. Making the cell the full width of the collection view doesn't show previous/next cells, and making the cell width smaller doesn't snap to center when paging.

Is is possible to make the collection view 80% of the screen width for example, and let the previous/next cells bleed outside the bounds (no clip to bounds)?

enter image description here

Or any other ideas to achieve this using the native paging?


Solution

  • I don't think there's an easy way to do this with the native paging enabled. But you can easily do a custom paging by utilising scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) to set the destination you want. By adding some logic to that you can create the pagination. You can find examples here and here