【问题标题】:Decrease Space between UICollectionViewCells减少 UICollectionViewCells 之间的空间
【发布时间】:2014-01-12 03:40:16
【问题描述】:

目标

我正在尝试减少 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;
}

然后在我的 UICollectionViewController viewDidLoad:

SublassedcollectionViewLayout *space = [[SublassedcollectionViewLayout alloc]init];
self.collectionView.collectionViewLayout = space;

问题

这只会让我的 UICollectionViewCells 更小。

编辑:我想减少从左到右而不是从上到下的间距。

任何帮助将不胜感激!

【问题讨论】:

  • @skladek 我不想在单元格上设置边距。我正在尝试减少一个单元格与另一个单元格之间的空间。
  • 这将通过增加单元格的大小或在左右添加边距来实现。集合视图的工作原理是取全宽,从每一侧减去边距,然后用剩余空间均匀地间隔元素。如果您希望单元格靠得更近,则需要减少剩余空间。

标签: ios uicollectionviewcell spacing uicollectionviewlayout


【解决方案1】:

垂直间距

在情节提要中选择您的收藏视图。

那就换吧……

变化

我还发现了这个answer,可能也有用

水平间距

对于从左到右的间距,根据我的测试,您只能更改从屏幕边缘到第一个单元格的距离,称为 Section Insets。您还可以使用 Min Spacing For Cells

更改单元格之间的最小间距,从 0 到 x

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

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

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

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

【讨论】:

  • 是的,这确实有效,但我想从左到右而不是从上到下减少。