美文网首页
通过autoLayout自适应tableviewCell高度

通过autoLayout自适应tableviewCell高度

作者: 夏日冰霜 | 来源:发表于2019-04-10 14:44 被阅读0次
    • 注意:本篇文章只适应于单个Label的cell自适应
    • 自适应cell高度,主要是UILabel的高度会有变化,所以这里主要是说说label变化时如何处理,设置UILabel的时候注意要设置preferredMaxLayoutWidth这个宽度,还有ContentHuggingPriority为UILayoutPriorityRequried
    tableviewCell具体代码:
    CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - 10 * 2;
        
        self.tempLabel = [UILabel getLabelWithFont:FMedium(20) textColor:C_Black_333333 superView:self.contentView masonrySet:^(UILabel *view, MASConstraintMaker *make) {
            
            make.top.equalTo(self.contentView).with.offset(10);
            make.left.equalTo(self.contentView).with.offset(10);
            make.right.equalTo(self.contentView).with.offset(-10);
            make.bottom.equalTo(self.contentView).with.offset(-10);
            
            view.numberOfLines = 0;
            view.preferredMaxLayoutWidth = maxWidth;
        }];
        
        [self.tempLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    
    
    tableviewDelegate返回高度的方法:
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return UITableViewAutomaticDimension;
    }
    

    支持版本为iOS 8以上的话才可以直接利用UITableViewAutomaticDimension在tableview的heightForRowAtIndexPath直接返回哦。如果需要iOS 8以下版本的自适应高度,私信给我。


    希望给个小心心支持一下😘~

    相关文章

      网友评论

          本文标题:通过autoLayout自适应tableviewCell高度

          本文链接:https://www.haomeiwen.com/subject/mrbniqtx.html