浅析collectionView的item间距

ios开发中比较常用到的就是 tableView 和 collectionView,二者分别用于列表展示网格展示;由于tableView的易用性几乎每个APP都会用到; 相对tableView,collectionView使用起来比较繁琐,所以collectionView的使用不常见。但UICollectionView也有自身的特点,比如横向布局、多行或者多列(瀑布流)展示。

一、创建collectionView(collectionView控制器)
1
2
3
4
5
(collectionView)
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
(collectionView控制器)
- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout ;

初始化类似tableView,设置delegate 和 datasource;
不同于tablView初始化设置UITableViewStyle,而collctionView初始化设置UICollectionViewLayout(一般用它的子类UICollectionViewFlowLayout)。
此处的UICollectionViewLayout是用于存储collectionView的一些布局属性:

1
2
3
4
5
@property (nonatomic) CGFloat minimumLineSpacing;
@property (nonatomic) CGFloat minimumInteritemSpacing;
@property (nonatomic) CGSize itemSize;
@property (nonatomic) UICollectionViewScrollDirection scrollDirection;
@property (nonatomic) UIEdgeInsets sectionInset;

cell间距的设置需要用到以上这些关键性属性。

二、布局协议

UICollectionViewDelegateFlowLayout

协议中有以下这几个和布局相关的方法

1
2
3
4
5
6
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;

这几个方法的功能和UICollectionViewLayout的4个属性基本相对应:
1.属性用于统一设置
2.对象方法既可以统一设置,也可以区别设置

这几个方法名的区别在后半部分

1
xxxxx sizeForItemAtIndexPath:(NSIndexPath *)indexPath

等同于属性 itemSize;

1
xxxxx insetForSectionAtIndex:(NSInteger)section

等同于属性 sectionInset;

1
xxxxx minimumLineSpacingForSectionAtIndex:(NSInteger)section

等同于属性 minimumLineSpacing;

1
xxxxx minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

等同于属性 minimumInteritemSpacing;

三、图文解析对应属性(针对不同滚动方向)

collectionView布局.gif
gif中每个不同颜色代表不同的section

水平方向滚动.png

水平方向滚动的collectionView,竖直方向的间距是固定的:minimumInteritemSpacing指的是同一个section 内部item的竖直方向间隙;
minimumLineSpacing指的是同一个section 内部 item的水平方向间隙;

竖直方向滚动.png

竖直方向滚动的collectionView,水平方向的间距是固定的:minimumInteritemSpacing指的是同一个section 内部item的水平方向间隙;
minimumLineSpacing指的是同一个section 内部 item的竖直方向间隙;

总结:

minimumInteritemSpacing表示 同一个section内部间item的 和滚动方向垂直方向的间距;

minimumLineSpacing指的是同一个section 内部 item 的滚动方向的间距;
sectionInset指的是每个section内缩进;属性设置的每个section的内错进是相同的,都是正数。如果需要实现不同的setion的不同的内缩进,可以使用对象方法

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

来实现。

注意:

在写demo的过程中,发现collectionView的contentInsetAdjustmentBehavior(等同于控制器的automaticallyAdjustsScrollViewInsets属性)默认为UIScrollViewContentInsetAdjustmentAutomatic枚举值,会自适应控制器的边距,避开导航栏和状态栏,而使得collectionView的真实高度 和设置的高度不一致(相差状态栏和导航栏的高度和)。

demo地址https://github.com/NightSkyWatchers/collectionView,欢迎下载观摩指正,内有*彩蛋*