当只有一个单元格时,单元格位于集合视图的中心

Cell is in the center of collection view when there is only one cell

我想从左到 right.So 布局单元格 我使用 UICollectionViewFlowLayout:

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// use this for let cell width fit for label width
layout.estimatedItemSize = CGSizeMake(80, 30);
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];

单元格的宽度适合标签的宽度:

// cell code
@implementation CQGoodsSearchCell

- (void)setupUI {
    self.contentView.layer.cornerRadius = 15;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.backgroundColor = [UIColor colorWithRed:1.00 green:1.00 blue:1.00 alpha:1.00];

    self.nameLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.nameLabel];
    self.nameLabel.font = [UIFont systemFontOfSize:15];
    self.nameLabel.textColor = [UIColor colorWithRed:0.18 green:0.18 blue:0.18 alpha:1.00];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.top.bottom.mas_offset(0);
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
    }];
}

当有多个单元格时一切顺利:

但是,当只有一个单元格时,单元格位于中心:

如果我用itemSize代替estimatedItemSize,就可以了:

layout.itemSize = CGSizeMake(80, 30);

我真的很困惑。将单元格置于集合视图的中心不是我想要的,但我也想使用自动布局,以便单元格的宽度自行调整。

那么有什么方法可以避免这种情况吗?

基本上这就是我在这里讨论的同一个问题:

总之,您尝试使用的功能自动调整集合视图单元格 不起作用,而且从未起作用。这就是为什么,正如你所说的那样,

If I use itemSize instead of estimatedItemSize, it will be OK

没错!这不是你的错误;这是一个 iOS 错误。自动调整集合视图单元格 不起作用 。应用程序崩溃或流程布局不正确。

这就是解决方案。不要使用 estimatedItemSize。相反,您自己计算正确的大小并在

的实现中提供它
func collectionView(_ collectionView: UICollectionView, 
    layout collectionViewLayout: UICollectionViewLayout, 
    sizeForItemAt indexPath: IndexPath) -> CGSize {
        // calculate/obtain correct size and return it
}

另外,使用自定义UICollectionViewLayout可以解决这个问题:

对于Objective-C:

@interface CQLeftSideLayout : UICollectionViewFlowLayout

@end

@implementation CQLeftSideLayout

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {

    NSArray *attributes = [super layoutAttributesForElementsInRect:rect];

    if (attributes.count == 1) {
        UICollectionViewLayoutAttributes *currentAttributes = attributes.firstObject;
        currentAttributes.frame = CGRectMake(self.sectionInset.left, currentAttributes.frame.origin.y, currentAttributes.frame.size.width, currentAttributes.frame.size.height);
    }

    return attributes;

}

@end

对于Swift:

class LeftSideLayout: UICollectionViewFlowLayout {

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

        let attributes = super.layoutAttributesForElements(in: rect)

        if attributes?.count == 1 {

            if let currentAttribute = attributes?.first {
                currentAttribute.frame = CGRect(x: self.sectionInset.left, y: currentAttribute.frame.origin.y, width: currentAttribute.frame.size.width, height: currentAttribute.frame.size.height)
            }

        }


        return attributes

    }

}

Select none 在“估计大小”选项中。如果集合视图只有一项,它将 left-aligned 而不是居中。