更改 CollectionView 单元格的垂直间距

Change Vertical Spacing of CollectionView Cell

我正在使用 UICollectionView 并像这样在集合视图的委托方法中更改项目的大小

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

    lastIndex = 0;
    layoutDiff = 4;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == (lastIndex + layoutDiff) - 1){
        layoutDiff = (layoutDiff == 4)?5:4;
        lastIndex = indexPath.row + 1;
        return CGSizeMake(213.2, 213.2);
    }

    return CGSizeMake(106.6, 106.6);
}

这是我当前的屏幕。

下面的所有单元格都遵循这种行为,这意味着我想要的模式就像一个集合视图项目一样大,这取决于我想把它变大的逻辑,但我不想要这个 space.我认为如果 space 不会出现,那么一切都会好起来的。有人可以帮忙吗?

FlowLayout 对此无能为力。

您必须实现 UICollectionLayout 的子类。

这里有一些关于自定义集合视图布局的文章。

自定义集合视图布局。

https://www.objc.io/issues/3-views/collection-view-layouts/

知道何时对流布局进行子类化 https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html#//apple_ref/doc/uid/TP40012334-CH3-SW4