问题
设计稿如下
在大部分机型上也没有问题,但是在
iPhone 12 Pro max 上,出现了下面的问题
如图
即cell之间的缝隙变大了
原因
看一下返回的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake((SCREEN_WIDTH - 15 * PLUS_SCALE * 5)/4,channelItemHeight);
}
上面的计算有一个问题,就是 通过 (SCREEN_WIDTH - 15 * PLUS_SCALE * 5)/4 计算得出的宽度并不总是精确的,会有一个误差,
如果这个宽度偏大了,就会导致collectionView. 的宽度无法容下4个cell
,所以这里应该加一个容错,就是将宽度稍微变小
解决方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake((SCREEN_WIDTH - 15 * PLUS_SCALE * 5)/4 - 0.0001 ,channelItemHeight);