好的,我已经这样做了;
在我的情况下坚持使用 288x180 的项目尺寸
1/.设置项目大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(288, 180);
}
2/.设置 insetForSectionAtIndex
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
//top, left, bottom, right
return UIEdgeInsetsMake(0, 16, 0, 16);
}
3/.设置 minimumInteritemSpacingForSectionAtIndex 以确保间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 5;
}
4/.创建单元格 ID 时执行以下操作;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
if (!cell)
{
cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
cell.contentView.width = 288;
cell.contentView.backgroundColor = [UIColor whiteColor];
}
return cell;
}
5/.要正确实现分页效果,请确保 collectionView.paging = NO 和子类 UICollectionViewFlowLayout 使用 targetContentOffsetForProposedContentOffset 正确设置滚动视图偏移量
不确定这是否是最好的方法,但它给了我想要的结果..