IOS15使用Masonry和自动计算Cell的高度
生活随笔
收集整理的這篇文章主要介紹了
IOS15使用Masonry和自动计算Cell的高度
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS15使用Masonry和自動計算Cell的高度
核心源碼
// 步驟1:tableView.rowHeight = UITableViewAutomaticDimension;// 步驟2:tableView.estimatedRowHeight = 100.0; CGFloat avatarButtonWidth = 32.0;self.avatarButton = [UIButton buttonWithType:UIButtonTypeCustom];self.avatarButton.layer.cornerRadius = avatarButtonWidth / 2.0;self.avatarButton.clipsToBounds = YES;[self.avatarButton setImage:[UIImage imageNamed:@"Avatar.jpg"] forState:UIControlStateNormal];[self.contentView addSubview:self.avatarButton];UIView *view = [[UIView alloc] init];self.nicknameLabel = [[UILabel alloc] init];self.nicknameLabel.font = [UIFont systemFontOfSize:13.0];self.nicknameLabel.textColor = [UIColor blackColor];self.nicknameLabel.text = @"昵稱";[view addSubview:self.nicknameLabel];self.timeLabel = [[UILabel alloc] init];self.timeLabel.font = [UIFont systemFontOfSize:12.0];self.timeLabel.textColor = [UIColor grayColor];self.timeLabel.text = @"4-27";[view addSubview:self.timeLabel];[self.contentView addSubview:view];self.contentLabel = [[UILabel alloc] init];self.contentLabel.numberOfLines = 0;self.contentLabel.font = [UIFont systemFontOfSize:15.0];self.contentLabel.textColor = [UIColor blackColor];[self.contentView addSubview:self.contentLabel];[self.avatarButton mas_makeConstraints:^(MASConstraintMaker *make) {// 步驟3:設置頭像的約束,最重要的思想是:給頭像的top與contentView.mas_top之間建立約束make.top.equalTo(self.contentView.mas_top).offset(27.0);// 設置size與leading(left)的約束,不解釋make.size.mas_equalTo(CGSizeMake(avatarButtonWidth, avatarButtonWidth));make.leading.equalTo(self.contentView.mas_leading).offset(20.0);}];// nicknameLabel與timeLabel也是使用了自動撐開的思想[self.nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.leading.top.equalTo(view);}];[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.leading.equalTo(self.nicknameLabel.mas_leading);make.top.equalTo(self.nicknameLabel.mas_bottom).offset(3.0);make.bottom.equalTo(view.mas_bottom);}];[view mas_makeConstraints:^(MASConstraintMaker *make) {make.leading.equalTo(self.avatarButton.mas_trailing).offset(10.0);make.trailing.lessThanOrEqualTo(self.contentView.mas_trailing).offset(-10.0);make.centerY.equalTo(self.avatarButton.mas_centerY);}];[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {// 步驟4:設置評論的top與頭像的bottom之間的約束make.top.equalTo(self.avatarButton.mas_bottom).offset(15.0);// 步驟5:設置評論的bottom與contentView.mas_bottom之間的約束make.bottom.equalTo(self.contentView.mas_bottom).offset(-25.0);make.leading.equalTo(self.avatarButton.mas_leading);make.trailing.equalTo(self.contentView.mas_trailing).offset(-20.0);}];
https://e.coding.net/lujun1/afnetworkinggetdemo/CellAutomaticHeight.git
總結
以上是生活随笔為你收集整理的IOS15使用Masonry和自动计算Cell的高度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCL 2.0发布,带来更强悍的异
- 下一篇: 文件结构