It provides 4 different type of Custom layout for CollectionView.
Two Cell Layout | Three Cell Left Layout | Three Cell Right Layout | Mix Layout |
---|---|---|---|
- iOS 9.0+
- Swift 4.0
Download the Code and Copy the layout file -> WBCollectionViewLayout.swift
into your project. That's it.
WBCollectionViewLayout is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'WBCollectionViewLayout', :git => 'https://github.com/mwaqasbhati/WBCollectionViewLayout.git'
To run the example project, clone the repo, and run pod install
from the Example directory first.
- Just Create a WBGridViewLayout object and Confirm to it's Delegate
let mlayout = WBGridViewLayout()
mlayout.delegate = self
collectionView.setCollectionViewLayout(layout, animated: true)
Note: Delegates methods are optional and in default case Mixture layout will be drawn but if you want more customization then you need to implement it's delegates
func colectionView(_ collectionView: UICollectionView, numberOfItemsInRow row: Int) -> CellLayout {
return .Two
}
func colectionView(_ collectionView: UICollectionView, numberOfItemsInRow row: Int) -> CellLayout {
return .ThreeLeft
}
func colectionView(_ collectionView: UICollectionView, numberOfItemsInRow row: Int) -> CellLayout {
return .ThreeRight
}
This Layout is the default one and you don't have to implement numberOfItemsInRow
delegate.
- If you want more customization in drawing the layout then you can optionally implement it's delegate methods given below which provides
numberOfItemsInRow
for each Row andsize of each Row
.
func colectionView(_ collectionView: UICollectionView, numberOfItemsInRow row: Int) -> CellLayout
func colectionView(_ collectionView: UICollectionView, sizeOfItemInRow row: Int) -> CGSize?
In case you load data asynchronously please follow next steps:
when data is loaded invalidate layout as well as reload data on collection view.
collectionView.collectionViewLayout.invalidateLayout()
collectionView.reloadData()
mwaqasbhati, [email protected]
WBCollectionViewLayout is available under the MIT license. See the LICENSE file for more info.