UICollectionView中的单元格间距 [英] Cell spacing in UICollectionView

查看:111
本文介绍了UICollectionView中的单元格间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 UICollectionView 的部分设置单元格间距?我知道有一个属性 minimumInteritemSpacing 我已经将它设置为5.0,但是间距没有出现5.0。我已经实现了flowout委托方法。

How do I set cell spacing in a section of UICollectionView? I know there is a property minimumInteritemSpacing I have set it to 5.0 still the spacing is not appearing 5.0. I have implemented the flowout delegate method.

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{

    return 5.0;
}

仍然没有得到理想的结果。我认为它的最小间距。我有没有办法设置最大间距?

still I am not getting the desired result. I think its the minimum spacing . Isn't there any way by which I can set the maximum spacing?

推荐答案

我知道这个话题已经过时了,但万一有人还需要正确答案你需要什么:

I know that the topic is old, but in case anyone still needs correct answer here what you need:


  1. 覆盖标准流程布局。

  2. 添加如下实现:

  1. Override standard flow layout.
  2. Add implementation like that:

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

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


其中maximumSpacing可能是设置为您喜欢的任何值。这个技巧保证了单元格之间的空间完全等于maximumSpacing !!

where maximumSpacing could be set to any value you prefer. This trick guarantees that the space between cells would be EXACTLY equal to maximumSpacing!!

这篇关于UICollectionView中的单元格间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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