masonry 实现tableview headerView高度

2022-11-11  本文已影响0人  大宝来巡山

设置headerview 约束

#import "EditorHeaderView.h"
#import <YYText.h>

- (void)bankViewInit{
    [self addSubview:self.titleLabel];
    [self addSubview:self.contentPromptLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(140);
    }];
  [self.contentPromptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(100);
        // 自适应高度
        make.bottom.equalTo(self);
    }];
  // self 自适应高度
    CGFloat height = [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

当headerView页面赋值需要改变frame高度的时候,最下面加上自适应高度代码

-(void)setAddressModel:(AddressModel *)addressModel{
    _addressModel = addressModel;
    if(addressModel != nil){
        self.addAddressBtn.hidden = YES;
        self.nameLabel.hidden = NO;
        self.phoneLabel.hidden = NO;
        self.addressInfoLabel.hidden = NO;
        self.addressSelectBtn.hidden = NO;
        self.changeAddressBtn.hidden = NO;
        NSString* name =  addressModel.consignee;
        //self.nameLabel.text = name;
        ///限制10个字
        if(name.length>10){
            name = [NSString stringWithFormat:@"%@...",[name substringToIndex:9]];
        }
        self.nameLabel.text = name;
        // CGFloat nameWidth = [name sizeWitLYFont:PingFangSCRegularFontWithSize(15) maxSize:CGSizeMake(1000, 20)].width;
        //  if(nameWidth >= 165){
        //       [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
       //           make.width.mas_lessThanOrEqualTo(@165);
       //       }];
       //   }
        self.phoneLabel.text = addressModel.phone;
        
        NSString *addressStr = [NSString stringWithFormat:@"%@%@%@%@%@",addressModel.provinceName?:@"",addressModel.cityName?:@"",addressModel.areaName?:@"",addressModel.detail?:@"",addressModel.roomNumber?:@""];
        
         //地址
         NSMutableAttributedString * addressAttributeText = [[NSMutableAttributedString alloc] initWithString:addressStr];
         addressAttributeText.yy_font = PingFangSCRegularFontWithSize(14);
         addressAttributeText.yy_color = [UIColor colorWithHexString:@"#333333"];
         //富文本图文混排
        if([addressModel.isDefault isEqualToString:@"1"]){
             self.defaultTag.hidden = NO;
            ///设置第一个字符偏移左边间距,从“默认”标签的右边开始显示文字
             addressAttributeText.yy_firstLineHeadIndent = kWPercentage(35);
         } else {
             self.defaultTag.hidden = YES;
             addressAttributeText.yy_firstLineHeadIndent = 0;
         }
        self.addressInfoLabel.attributedText = addressAttributeText;
        [self setNeedsLayout];
        [self layoutIfNeeded];
    }else{
        self.addAddressBtn.hidden = NO;
        self.nameLabel.hidden = YES;
        self.phoneLabel.hidden = YES;
        self.addressInfoLabel.hidden = YES;
        self.addressSelectBtn.hidden = YES;
        self.changeAddressBtn.hidden = YES;
        ///增加bottom 约束定高度
        [self.addAddressBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-RealValue(32));
        }];
    }
   //重点来了:self 自适应高度
   CGFloat height = [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
   CGRect frame = self.frame;
   frame.size.height = height;
   self.frame = frame;

}

将headerview设置为tableview头视图

    EditorHeaderView *headerView = [[EditorHeaderView alloc] init];
    headerView. addressModel = self. addressModel;
    self.tableView.tableHeaderView = headerView;
上一篇下一篇

猜你喜欢

热点阅读