How do I set the height of tableHeaderView (UITableView) with autolayout?

后端 未结 12 1244
后悔当初
后悔当初 2020-11-27 10:23

I\'m been smashing my head against the wall with this for last 3 or 4 hours and I can\'t seem to figure it out. I have a UIViewController with a full screen UITableView insi

12条回答
  •  离开以前
    2020-11-27 11:01

    For iOS 12 and above, the following steps will ensure autolayout works properly in both your table and the header.

    1. Create your tableView first, then the header.
    2. At the end of your header creation code, call:
    [headerV setNeedsLayout];
    [headerV layoutIfNeeded];
    
    1. Upon orientation change, mark the header for layout again and reload the table, this needs to happen slightly after the orientation change is reported:
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 *NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    
      [tableV.tableHeaderView setNeedsLayout];
      [tableV.tableHeaderView layoutIfNeeded];
      [tableV reloadData];});
    

提交回复
热议问题