UICollectionView中不同宽度的item保持相同的距离


继承UICollectionViewLayout重写布局,使collectionView中不同宽度的item,每个item保持相同的距离,不会等分,剩余的位置会在每行的尾部。

实现原理:继承UICollectionViewLayout,重写四个方法

1、布局cell位置

- (void)prepareLayout

2、返回指定范围的cell的布局的详细信息

- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect

3、返回指定cell的详细布局信息

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

4、重新计算当前滚动视图的contentSize

- (CGSize)collectionViewContentSize


注意:在声明中写个协议表明用户必须实现下面方法,这样才能在上面的方法中获取item(cell)的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;


如果不想重写可点击下面的连接进入网页下载,下载后拉到工程中可直接使用:

跳转到下载地址

你可能感兴趣的:(UICollectionView中不同宽度的item保持相同的距离)