UITableView tableHeaderView autolayout自动计算高度


1.代码方式

//测试代码
@interface ViewController ()<UITableViewDataSource> 

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic)  UILabel *headerLable;

@property (strong, nonatomic)  UIView *headerView;

@end

 

 

    //Auto Layout能够计算出正确的界面的必要条件是约束充分
self.headerLable = [UILabel new]; self.headerLable.numberOfLines = 0; self.headerView = [UIView new]; [self.headerView addSubview:self.headerLable]; [self.headerLable mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.headerView).valueOffset([NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)]); }]; self.tableView.tableHeaderView = self.headerView; [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(self.view.mas_width); }]; self.headerLable.text = @"测试文本测试文本测试文本测试文本测试文本测试文本测试文本测试文本测试文本测试文本";
[self.tableView.tableHeaderView layoutIfNeeded]; self.tableView.tableHeaderView
= self.headerView;

⚠ 1.修改tableView的tableHeaderView界面时,界面并不能及时刷新,可手动调用layoutIfNeeded 此时view高度为理想高度

    2.tableViewHeader高度变化时,界面也不能及时刷新 需要重新将headerView设置为tableView的tableHeaderView,界面即可正确显示了

    3.当然,使用auto layout时,约束条件必须充分 才能计算出正确高度哦^^

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM