UICollectionView
在 Apple 文档中本指南的流程布局部分,在“指定项目和行之间的空间”下,我们读到:
在布局期间,流布局对象将项目添加到当前行,直到没有足够的空间来容纳整个项目。如果该行刚好足以容纳整数个没有额外空间的项目,那么项目之间的空间将等于最小间距。如果行尾有多余的空间,布局对象会增加项目间的间距,直到项目均匀地放在行边界内,如图 3-3 所示。增加间距可以改善项目的整体外观并防止每行末尾出现大的间隙。
(强调我的)
但这似乎不是真的。从 iOS 12.4 和 iOS 13 beta 7 开始,如果您使用未填充可用宽度的小单元格制作垂直滚动的集合视图,则项目间间距不会从最小值扩展以填充宽度。请参阅此示例,其中包含两个部分,每个部分 5 个项目,20pt x 20pt,最小项目间距、最小行间距和部分插入全部为 4 pt:
文档是否已过时?或者我可能在做一些奇怪的事情?这是一个示例应用程序,它只包含一个集合视图,其委托/数据源使用UICollectionViewCells
仅具有背景颜色的 vanilla,并实现上述大小调整方法,仅此而已。
代码:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 5;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor lightGrayColor];
return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(20, 20);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(4, 4, 4, 4);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 4;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 4;
}