自动布局小知识--UITableViewCell自定义高度,Label长度自适应

    xiaoxiao2026-09-09  7

    在自动布局时代,自定义cell设置高度没必要再去通过先传一遍参数,预先计算出cell的高度,再去设置参数来解决了;

    可以通过设置tableView的两个属性:

            self.estimatedRowHeight = 44;

            self.rowHeight = UITableViewAutomaticDimension;

    然后注意自定义cell里面要去修改cell.contentView的约束,使cell.contentView自适应内容的高度。不用通过tableView的代理设置高度。

    关于Label自适应宽度的话,需要预先设置label的内容,然后在自动布局中设置

    [labSeller mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(labDevide.mas_top).offset(12);

            make.leading.equalTo(self.contentView.mas_leading).offset(15);

            make.width.equalTo(@([labSeller systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].width));

        }];

    通过 systemLayoutSizeFittingSize : 方法去获得实际的宽度大小
    转载请注明原文地址: https://ju.6miu.com/read-1311916.html
    最新回复(0)