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

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

问题描述

如何在 UICollectionView 的一部分中设置单元格间距?我知道有一个属性 minimumInteritemSpacing 我已将其设置为 5.0,但间距仍未显示为 5.0.我已经实现了流出委托方法.

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天全站免登陆