ASJCollectionViewFillLayout
标准的 UICollectionViewLayout
实现得很好,但UI的对称性可能取决于 UICollectionView
显示的项目数量。这个库试图解决这个问题。这是一个填充集合视图全宽度的 UICollectionViewLayout
子类。
安装
CocoaPods是安装此库的首选方式。将以下命令添加到您的Podfile
pod 'ASJCollectionViewFillLayout'
如果您喜欢经典方式,只需将 ASJCollectionViewFillLayout
文件夹 (.h 和 .m 文件) 复制到您的项目中。
使用方法
创建 ASJCollectionViewFillLayout
很简单。它有一个简单的接口,包含三个属性。您还可以使用传统的代理模式来返回您希望使用的属性。所有这些都是可选的。
@property (assign, nonatomic) NSInteger numberOfItemsInSide;
根据布局的 direction
属性,设置一排或一列中显示的项目数量。
@property (assign, nonatomic) CGFloat itemLength;
根据布局的 direction
属性,设置项目的宽度和高度。对于垂直方向,这设置为项目高度,对于水平方向,这设置为项目宽度。
@property (assign, nonatomic) CGFloat itemSpacing;
设置两个集合视图项目之间的距离。
@property (assign, nonatomic) ASJCollectionViewFillLayoutDirection direction;
设置集合视图项的布局方向。有效选项有ASJCollectionViewFillLayoutVertical
和ASJCollectionViewFillLayoutHorizontal
。默认为ASJCollectionViewFillLayoutVertical
。
@property (assign, nonatomic) BOOL stretchesLastItems;
设置最后几项是否应拉伸以填充UICollectionView
的完整宽度。默认为YES
。
例如
ASJCollectionViewFillLayout *aFillLayout = [[ASJCollectionViewFillLayout alloc] init];
aFillLayout.numberOfItemsInSide = 3;
aFillLayout.itemSpacing = 5.0f;
aFillLayout.itemLength = 75.0f;
aFillLayout.stretchesLastItems = NO;
aCollectionView.collectionViewLayout = aFillLayout;
请参阅示例项目,了解如何使用委托模式。
致谢
- Damir Tursunovic - 实现UICollectionViewLayout
- Oggerschummer为添加
stretchesLastItems
属性
待办事项
处理总集合视图项少于一行中的项数的情况。添加水平方向的选项;尝试 subclassingUICollectionViewFlowLayout
本身。
许可
ASJCollectionViewFillLayout
在MIT许可下提供。有关更多信息,请参阅LICENSE文件。