我正在尝试UICollectionViewCells使用自动布局进行自我调整,但我似乎无法让单元格根据内容调整自己的大小.我无法理解如何根据单元格内容中的内容更新单元格的大小.
这是我尝试过的设置:
UICollectionViewCell一个UITextView.UITextView已禁用.UITextView固定在单元格左侧,显式宽度为320.UITextView固定到单元格的顶部.UITextView具有高度限制设置为其中一个恒定的UITextView报告将适合文本.这是单元格背景设置为红色,UITextView背景设置为蓝色的样子:

我把我已经在GitHub上玩的项目在这里.
我想从左到右布局单元格。所以我使用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];
Run Code Online (Sandbox Code Playgroud)
单元格的宽度适合标签的宽度:
// 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);
}];
} …Run Code Online (Sandbox Code Playgroud)