https://github.com/devxoul/uicollectionviewflexlayout
A drop-in replacement for UICollectionViewFlowLayout
https://github.com/devxoul/uicollectionviewflexlayout
uicollectionview uicollectionviewlayout
Last synced: 4 months ago
JSON representation
A drop-in replacement for UICollectionViewFlowLayout
- Host: GitHub
- URL: https://github.com/devxoul/uicollectionviewflexlayout
- Owner: devxoul
- License: mit
- Created: 2017-08-04T17:58:55.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-04-03T15:02:00.000Z (over 6 years ago)
- Last Synced: 2025-08-10T23:42:48.764Z (4 months ago)
- Topics: uicollectionview, uicollectionviewlayout
- Language: Swift
- Homepage:
- Size: 34.2 KB
- Stars: 306
- Watchers: 7
- Forks: 21
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# UICollectionViewFlexLayout

[](https://cocoapods.org/pods/UICollectionViewFlexLayout)
[](https://travis-ci.org/devxoul/UICollectionViewFlexLayout)
[](https://codecov.io/gh/devxoul/UICollectionViewFlexLayout)
UICollectionViewFlexLayout is a drop-in replacement for UICollectionViewFlowLayout.
## Features
* [x] Section Spacing
* [x] Section Margin
* [x] Section Padding
* [x] Section Background
* [x] Item Spacing
* [x] Item Margin
* [x] Item Padding
* [x] Item Size
* [x] Item Background
* [x] Item Z-Index
## Basic Concept
Don't let cells have margins and paddings. Cell metrics are now set outside of the cell. Just focus on contents.

## Usage
### UICollectionViewDelegateFlexLayout
```swift
protocol UICollectionViewDelegateFlexLayout {
// section vertical spacing
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewFlexLayout, verticalSpacingBetweenSectionAt section: Int, and nextSection: Int) -> CGFloat
// section margin
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewFlexLayout, marginForSectionAt section: Int) -> UIEdgeInsets
// section padding
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewFlexLayout, paddingForSectionAt section: Int) -> UIEdgeInsets
// item horizontal spacing
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewFlexLayout, horizontalSpacingBetweenItemAt indexPath: IndexPath, and nextIndexPath: IndexPath) -> CGFloat
// item vertical spacing
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewFlexLayout, verticalSpacingBetweenItemAt indexPath: IndexPath, and nextIndexPath: IndexPath) -> CGFloat
// item margin
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewFlexLayout, marginForItemAt indexPath: IndexPath) -> UIEdgeInsets
// item padding
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewFlexLayout, paddingForItemAt indexPath: IndexPath) -> UIEdgeInsets
// item size
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewFlexLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
// item z-index
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewFlexLayout, zIndexForItemAt indexPath: IndexPath) -> Int
}
```
### Section and Item Background
```swift
// register
collectionView.register(MySectionBackgroundView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionBackground, withReuseIdentifier: "mySectionBackgroundView")
collectionView.register(MyItemBackgroundView.self, forSupplementaryViewOfKind: UICollectionElementKindItemBackground, withReuseIdentifier: "myItemBackgroundView")
// configure
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionBackground: // section background
return collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionBackground, withReuseIdentifier: "mySectionBackgroundView", for: indexPath)
case UICollectionElementKindItemBackground: // item background
return collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindItemBackground, withReuseIdentifier: "myItemBackgroundView", for: indexPath)
case foo: // else
return bar
}
}
```
## Tips and Tricks
* **Using with RxCocoa**
If you're using UICollectionView with RxSwift and RxCocoa, you should create an extension of `_RXDelegateProxy` class to support delegate proxy.
```swift
import RxCocoa
import UICollectionViewFlexLayout
extension _RXDelegateProxy: UICollectionViewDelegateFlexLayout {
}
```
## Contributing
```console
$ swift package generate-xcodeproj
```
## License
UICollectionViewFlexLayout is under MIT license. See the [LICENSE](LICENSE) file for more info.