减少UICollectionViewCells之间的空间

Abd*_*que 0 spacing ios uicollectionviewcell uicollectionviewlayout

目的

我试图减少我的UICollectionViewCells之间的空间

我尝试了什么

我尝试了子类化UICollectionViewFlowLayout并重写此方法:

- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];

    for(int i = 1; i < [answer count]; ++i) {
        UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
        UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
        NSInteger maximumSpacing = 4;
        NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
        if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
            CGRect frame = currentLayoutAttributes.frame;
            frame.origin.x = origin + maximumSpacing;
            currentLayoutAttributes.frame = frame;
        }
    }
    return answer;
}
Run Code Online (Sandbox Code Playgroud)

然后在我的UICollectionViewController viewDidLoad中:

SublassedcollectionViewLayout *space = [[SublassedcollectionViewLayout alloc]init];
self.collectionView.collectionViewLayout = space;
Run Code Online (Sandbox Code Playgroud)

问题

这只会让我的UICollectionViewCells更小.

编辑:我想从左到右而不是从上到下减小间距.

任何帮助将不胜感激!

Dog*_*fee 6

垂直间距

在故事板中选择您的集合视图.

然后换......

在此输入图像描述

更改

在此输入图像描述

在此输入图像描述

我也发现这个答案,也可能有用

水平间距

对于从左到右的间距,从我的测试中,您只能更改从屏幕边缘到第一个单元格的距离,称为" 截面插入".您还可以使用Min Spacing For Cells更改单元格之间的最小间距,从0到x

将集合视图设置为这些设置会产生

在此输入图像描述

在此输入图像描述

将最小单元格间距更改为:

在此输入图像描述

在此输入图像描述

由此看来,您只能从第一个和最后一个单元格调整左侧和右侧(填充)的插入,然后调整单元格之间的最小距离.其余的是自动计算的.

因此,为了获得所需的效果,您需要更改这些值,Min Spacing For CellsSection Insets