UICollectionViewFlexLayout
UICollectionViewFlexLayout 是 UICollectionViewFlowLayout 的即时替代品。
特性
- 分区间距
- 分区边距
- 分区填充
- 分区背景
- 项目间距
- 项目边距
- 项目填充
- 项目大小
- 项目背景
- 项目 Z 轴索引
基本概念
不要让单元格有边距和填充。单元条的尺寸现在在单元条之外设置。只需关注内容。
使用方式
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
}
分区和项目背景
// 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
}
}
技巧与窍门
-
与RxCocoa一起使用
如果你在使用与RxSwift和RxCocoa配合的UICollectionView,你应该扩展
_RXDelegateProxy
类以支持代理委托。import RxCocoa import UICollectionViewFlexLayout extension _RXDelegateProxy: UICollectionViewDelegateFlexLayout { }
贡献
$ swift package generate-xcodeproj
许可
UICollectionViewFlexLayout遵循MIT许可。更多信息请参阅<ンク href="LICENSE">"LICENSE"文件。