如果只有一个单元格,则该单元格位于集合视图的中心 [英] Cell is in the center of collection view when there is only one cell

查看:66
本文介绍了如果只有一个单元格,则该单元格位于集合视图的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从左到右布局单元格,所以我使用 UICollectionViewFlowLayout :

I want to layout cells from left to right.So I use 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];

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

The cell's width is fit for the label's width:

// 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);
    }];
}

一个以上的单元格时一切都很好:

Everything gose well when there is more than one cell:

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

However,the cell is in the center when there is only one cell:

如果我使用 itemSize 而不是 estimatedItemSize ,就可以了:

If I use itemSize instead of estimatedItemSize, it is OK:

layout.itemSize = CGSizeMake(80, 30);

我对此感到非常困惑.将单元格置于集合视图的中心不是我想要的,但我也想使用自动布局",以便单元格的宽度自行调整.

I am really confused about this. Having the cell in the center of the collection view is not what I want but I also want to use Auto Layout so the cell's width adjusts itself.

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

So is there any way to avoid this?

推荐答案

基本上,这是我在此处讨论的相同问题:

Basically this is the same issue I discuss here:

https://stackoverflow.com/a/52428617/341994

总而言之,您要使用的功能自动调整尺寸的收集视图单元格无效,并且从未使用过.就像您正确说的那样,这就是为什么

In sum, the feature you are trying to use, self-sizing collection view cells, doesn't work and has never worked. That is why, as you correctly say,

如果我使用 itemSize 而不是 estimatedItemSize ,就可以了

是的!这不是你的虫子.这是一个iOS错误.自动调整大小的集合视图单元不起作用.应用程序崩溃或流布局不正确.

That's right! It isn't your bug; it's an iOS bug. Self-sizing collection view cells do not work. Either the app crashes or the flow layout is incorrect.

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

So that's the solution. Do not use estimatedItemSize. Instead, calculate the correct size yourself and provide it in your implementation of

func collectionView(_ collectionView: UICollectionView, 
    layout collectionViewLayout: UICollectionViewLayout, 
    sizeForItemAt indexPath: IndexPath) -> CGSize {
        // calculate/obtain correct size and return it
}

这篇关于如果只有一个单元格,则该单元格位于集合视图的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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