减少UICollectionViewCells之间的空间 [英] Decrease Space between UICollectionViewCells

查看:108
本文介绍了减少UICollectionViewCells之间的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标



我正在尝试减少我的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;
}
}
返回答案;
}

然后在我的UICollectionViewController viewDidLoad中:

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



问题



这只是让我的UICollectionViewCells我希望从左到右而不是从上到下减小间距。



任何帮助将不胜感激!

解决方案

垂直间距



在故事板中选择您的收藏视图。



然后更改...





更改









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



HORIZ ONTAL SPACING



对于从左到右的间距,从我的测试中你只能改变从屏幕边缘到第一个单元格的距离,称为截面插入即可。您还可以使用单元格的最小间距

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

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







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







从这看来,你只能调整左边和右边的填充(填充),从第一个和最后一个单元格开始,然后调整单元格之间的最小距离。其余的是自动计算的。



为了获得所需的效果,您需要更改这些值,细胞的最小间距部分插入


Objective

I am trying to decrease the space between my UICollectionViewCells

What I tried

I tried subclassing UICollectionViewFlowLayout and overriding this method:

- (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;
}

and then in my UICollectionViewController viewDidLoad:

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

Problem

That just makes my UICollectionViewCells smaller.

EDIT: I want to decrease the spacing from left to right instead of top to bottom.

Any help will be appreciated!

解决方案

VERTICAL SPACING

Select your collection view in the storyboard.

Then change...

Changing

To

I also found this answer, might also be useful

HORIZONTAL SPACING

For Left to Right Spacing, from my testing you can only change the distance from the screen edge to first cell, called Section Insets. You can also change the min spacing between the cells, from 0 to x using Min Spacing For Cells

Setting the collection view to these settings produces

Changing the Min Cell Spacing to:

From this, it appears that you can only adjust the insets on the left and right (padding) from the first and last cell and then the minimum distance between cell. The rest is automatically calculated.

So to get the desired effect you need to alter these values, Min Spacing For Cells and the Section Insets

这篇关于减少UICollectionViewCells之间的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆