Swiftpack.co - Package - devxoul/UICollectionViewFlexLayout

UICollectionViewFlexLayout

Swift CocoaPods Build Status Codecov

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.

idea

Usage

UICollectionViewDelegateFlexLayout

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

// 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 RxCollectionViewDelegateProxy to support delegate proxy.

    import RxCocoa
    import UICollectionViewFlexLayout
    
    extension RxCollectionViewDelegateProxy: UICollectionViewDelegateFlexLayout {
    }
    

Contributing

$ swift package generate-xcodeproj

License

UICollectionViewFlexLayout is under MIT license. See the LICENSE file for more info.

Github

link
Stars: 177
Help us keep the lights on

Dependencies

Releases

1.1.0 - Jun 13, 2018

  • (Breaking Change) Make section background fill the entire section width (#5)

1.0.1 - Nov 12, 2017

  • Improve prepare() performance (d0665844ef6f1d0cd68387805c4b1ae350d2b435)

1.0.0 - Sep 25, 2017

  • Add Swift 4 support ✨

0.3.1 - Sep 9, 2017

  • Make util methods public (8736b3060ad6085ecf5458e975a9936bfb022dcb)

0.3.0 - Aug 30, 2017

  • Add support for z-index (f81f044a3208f2965da4554ecff28353428476d9, 176ebc667433208f9a26be200d46b02ef9dde0e8)