Custom page size paging for UICollectionView — supports page dimensions smaller than the collection view itself.
- When your desired page size is smaller than the collection view's frame,
UICollectionView.isPagingEnabledwon't work correctly.ZLCollectionFreePageLayoutsolves this with a custom snap-to-page scroll behavior. - You do not need to set
collectionView.isPagingEnabled = true. Paging is handled automatically by the layout. - Supports both
.horizontaland.verticalscroll directions.
| Component | Minimum |
|---|---|
| iOS | 11.0+ |
| Swift | 5.0+ |
pod 'ZLCollectionFreePageLayout'
# or pin a version:
pod 'ZLCollectionFreePageLayout', '~> 0.0.7'Download or clone the repo, then drag ZLCollectionFreePageLayout/ZLCollectionFreePageLayout.swift into your project.
- Import the module:
import ZLCollectionFreePageLayout- Create and configure a
ZLCollectionFreePageLayoutinstance, then assign it to your collection view:
private lazy var collectionView: UICollectionView = {
let layout = ZLCollectionFreePageLayout()
// Custom page width (horizontal) or page height (vertical)
layout.pageWOrH = 200.0
layout.scrollDirection = .horizontal // or .vertical
layout.minimumLineSpacing = 10.0
layout.minimumInteritemSpacing = 10.0
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.showsHorizontalScrollIndicator = false
return cv
}()- That's it — no need to set
isPagingEnabled.
| Property | Type | Default | Description |
|---|---|---|---|
pageWOrH |
CGFloat |
0 |
Custom page width (horizontal) or height (vertical). 0 falls back to the collection view's own width/height, equivalent to standard isPagingEnabled. |
scrollDirection |
UICollectionView.ScrollDirection |
.vertical |
Inherited from UICollectionViewFlowLayout. Determines whether pageWOrH controls width or height. |
minimumLineSpacing |
CGFloat |
10 |
Spacing between pages along the scroll direction. |