自定义UICollection布局

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布局。