自定义UICollection布局

Demo地址(支持pod)


UICollectionViewFlowLayout

在设置CollectionView的布局对象的时候需要设置size以保证每个Item的大小可以提前布局。

当我们需要自定义布局的时候需要重写UICollectionViewLayout的方法

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;

//不规则布局需要计算每个Item的大小 布局的次数可能会超过Item总数量次数

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;

以及UICollectionViewDelegateFlowLayout的代理方法

//不规则的自定义布局需要一个一个的计算标签的宽度 不能一次性返回 相比等分的样式需要更多次数的布局计算



- (CGSize)collectionView:(UICollectionView *)collectionView

layout:(UICollectionViewLayout *)collectionViewLayout

sizeForItemAtIndexPath:(NSIndexPath *)indexPath ;

//返回间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;

//返回Inset

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;


借鉴了两个例子做了一个Demo(github有链接),demo是完全基于Masonry自动布局的不支持frame布局。

Demo地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容