TsinHzl/ZLCollectionFreePageLayout

Set custom page size(width/height) especially for the size which is less than the width/height of collection view.设置page width或height小于collection view width或height的布局

★ 3Forks 0SwiftGitHub ↗Compare

README

ZLCollectionFreePageLayout

Custom page size paging for UICollectionView — supports page dimensions smaller than the collection view itself.

中文版

ScreenRecord

screen record

Description

  • When your desired page size is smaller than the collection view's frame, UICollectionView.isPagingEnabled won't work correctly. ZLCollectionFreePageLayout solves 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 .horizontal and .vertical scroll directions.

Requirements

Component Minimum
iOS 11.0+
Swift 5.0+

Installation

CocoaPods
pod 'ZLCollectionFreePageLayout'
# or pin a version:
pod 'ZLCollectionFreePageLayout', '~> 0.0.7'
Manual

Download or clone the repo, then drag ZLCollectionFreePageLayout/ZLCollectionFreePageLayout.swift into your project.

Usage

  1. Import the module:
import ZLCollectionFreePageLayout
  1. Create and configure a ZLCollectionFreePageLayout instance, 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
}()
  1. That's it — no need to set isPagingEnabled.

API

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.

Contributors

TsinHzlharlan-qq

Issues