HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 你如何确定UICollectionView flowLayout中的单元格之间的间距大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_2@
我有一个UICollectionView与流布局和每个单元格是一个正方形。如何确定每行上每个单元格之间的间距?我似乎找不到适当的设置。我看到在集合视图的nib文件一个最小间距属性,但我设置为0,单元格甚至不粘。

任何其他想法?

解决方法

更新:Swift版本的这个答案: https://github.com/fanpyi/UICollectionViewLeftAlignedLayout-Swift

采取@matt’s lead修改他的代码,以确保项目总是左对齐。我发现如果一个项目自己在一行上结束,它将以流布局为中心。@R_551_10673@下更改以@L_772_14@此问题。

这种情况只会发生,如果你有宽度不同的单元格,这可能导致一个布局,如下所示。最后一行总是左对齐由于UICollectionViewFlowLayout的行为,问题在于项本身是在任何行,但最后一个

使用@ matt的代码,我看到。

在这个例子中,我们看到,如果细胞自己在线上结束,它们将居中。下面的代码确保您的集合视图看起来像这样

#import "CWDLeftAlignedCollectionViewFlowLayout.h"

const NSInteger kMaxCellSpacing = 9;

@implementation CWDLeftAlignedCollectionViewFlowLayout

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect];
    for (UICollectionViewLayoutAttributes* attributes in attributesToReturn) {
        if (nil == attributes.representedElementKind) {
            NSIndexPath* indexPath = attributes.indexPath;
            attributes.frame = [self layoutAttributesForItemATindexPath:indexPath].frame;
        }
    }
    return attributesToReturn;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemATindexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes* currentItemAttributes =
    [super layoutAttributesForItemATindexPath:indexPath];

    UIEdgeInsets sectionInset = [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout sectionInset];

    if (indexPath.item == 0) { // first item of section
        CGRect frame = currentItemAttributes.frame;
        frame.origin.x = sectionInset.left; // first item of the section should always be left aligned
        currentItemAttributes.frame = frame;

        return currentItemAttributes;
    }

    NSIndexPath* prevIoUsIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section];
    CGRect prevIoUsFrame = [self layoutAttributesForItemATindexPath:prevIoUsIndexPath].frame;
    CGFloat prevIoUsFrameRightPoint = prevIoUsFrame.origin.x + prevIoUsFrame.size.width + kMaxCellSpacing;

    CGRect currentFrame = currentItemAttributes.frame;
    CGRect strecthedCurrentFrame = CGRectMake(0,currentFrame.origin.y,self.collectionView.frame.size.width,currentFrame.size.height);

    if (!CGRecTintersectsRect(prevIoUsFrame,strecthedCurrentFramE)) { // if current item is the first item on the line
        // the approach here is to take the current frame,left align it to the edge of the view
        // then stretch it the width of the collection view,if it intersects with the prevIoUs frame then that means it
        // is on the same line,otherwise it is on it's own new line
        CGRect frame = currentItemAttributes.frame;
        frame.origin.x = sectionInset.left; // first item on the line should always be left aligned
        currentItemAttributes.frame = frame;
        return currentItemAttributes;
    }

    CGRect frame = currentItemAttributes.frame;
    frame.origin.x = prevIoUsFrameRightPoint;
    currentItemAttributes.frame = frame;
    return currentItemAttributes;
}

@end
@H_403_2@

大佬总结

以上是大佬教程为你收集整理的iphone – 你如何确定UICollectionView flowLayout中的单元格之间的间距全部内容,希望文章能够帮你解决iphone – 你如何确定UICollectionView flowLayout中的单元格之间的间距所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。