相关疑难解决方法(0)

具有自动布局的UICollectionView自定义单元格

我正在尝试UICollectionViewCells使用自动布局进行自我调整,但我似乎无法让单元格根据内容调整自己的大小.我无法理解如何根据单元格内容中的内容更新单元格的大小.

这是我尝试过的设置:

  • 在其contentView中自定义UICollectionViewCell一个UITextView.
  • 滚动显示UITextView已禁用.
  • contentView的水平约束是:"H:| [_textView(320)]",即UITextView固定在单元格左侧,显式宽度为320.
  • contentView的垂直约束是:"V:| -0 - [_ textView]",即UITextView固定到单元格的顶部.
  • UITextView具有高度限制设置为其中一个恒定的UITextView报告将适合文本.

这是单元格背景设置为红色,UITextView背景设置为蓝色的样子: 细胞背景红色,UITextView背景蓝色

我把我已经在GitHub上玩的项目在这里.

ios autolayout uicollectionview uicollectionviewcell

192
推荐指数
7
解决办法
20万
查看次数

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

我想从左到右布局单元格。所以我使用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)

objective-c ios autolayout uicollectionview

3
推荐指数
2
解决办法
1038
查看次数