1. Code mode
// Test Code @interface Viewcontroller () <UITableViewDataSource>*TableView; @property (Strong, nonatomic) * headerlable, @property (Strong, nonatomic) *Headerview; @end
Auto layout is able to calculate the correct interface is required to be constrained sufficiently
self.headerlable = [UILabelNew]; Self.headerLable.numberOfLines=0; Self.headerview= [UIViewNew]; [Self.headerview addSubview:self.headerLable]; [Self.headerlable mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.edges.mas_equalTo (Self.headerview). Valueoffset ([Nsvalue valuewithuiedgeinsets:uiedgeinsetsmake (Ten,Ten,Ten,Ten)]); }]; Self.tableView.tableHeaderView=Self.headerview; [Self.headerview mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.width.mas_equalTo (self.view.mas_width); }]; Self.headerLable.text=@"test Text Test text Test text test textual testing text Test text Test text Test text Test text Test text test texts";
[Self.tableView.tableHeaderView layoutifneeded]; Self.tableView.tableHeaderView= Self.headerview;
? 1. When modifying the TableView Tableheaderview interface, the interface does not refresh in time and can be called manually layoutifneeded the view height is the ideal height
2.tableViewHeader height changes, the interface can not be refreshed in time need to re-set the Headerview to TableView Tableheaderview, the interface can be displayed correctly
3. Of course, when using auto layout, the constraints must be sufficient to calculate the correct height oh ^ ^
UITableView tableheaderview AutoLayout Automatic height calculation