title: How do I set the height of tableHeaderView (UITableView) with autolayout?
body: <p>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 inside of it (there's some other stuff on the screen, which is why I can't use a UITableViewController) and I want to get my tableHeaderView to resize with autolayout. Needless to say, it's not cooperating.</p>
  
  <p>See screenshot below.</p>
  
  <p><img src="https://i.stack.imgur.com/x0QoU.png" alt="enter image description here"></p>
  
  <p>Because the overviewLabel (e.g. the "List overview information here." text) has dynamic content, I'm using autolayout to resize it and it's superview. I've got everything resizing nicely, except for the tableHeaderView, which is right below Paralax Table View in the hiearchy. </p>
  
  <p>The only way I've found to resize that header view is programatically, with the following code:</p>
  
  <pre><code>CGRect headerFrame = self.headerView.frame;
  headerFrame.size.height = headerFrameHeight;
  self.headerView.frame = headerFrame;
  [self.listTableView setTableHeaderView:self.headerView];
  </code></pre>
  
  <p>In this case, headerFrameHeight is a manual calculation of the tableViewHeader height as follows <em>(innerHeaderView is the white area, or the second "View", headerView is tableHeaderView)</em>: </p>
  
  <pre><code>CGFloat startingY = self.innerHeaderView.frame.origin.y + self.overviewLabel.frame.origin.y;
  CGRect overviewSize = [self.overviewLabel.text
                         boundingRectWithSize:CGSizeMake(290.f, CGFLOAT_MAX)
                         options:NSStringDrawingUsesLineFragmentOrigin
                         attributes:@{NSFontAttributeName: self.overviewLabel.font}
                         context:nil];
  CGFloat overviewHeight = overviewSize.size.height;
  CGFloat overviewPadding = ([self.overviewLabel.text length] &gt; 0) ? 10 : 0; // If there's no overviewText, eliminate the padding in the overall height.
  CGFloat headerFrameHeight = ceilf(startingY + overviewHeight + overviewPadding + 21.f + 10.f);
  </code></pre>
  
  <p>The manual calculation works, but it's clunky and prone to error if things change in the future. What I want to be able to do is have the tableHeaderView auto-resize based on the provided constraints, like you can anywhere else. But for the life of me, I can't figure it out.</p>
  
  <p>There's several posts on SO about this, but none are clear and ended up confusing me more. Here's a few:</p>
  
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/14637376/auto-layout-on-uitableviewheader">Auto layout on UITableViewHeader</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/18281691/auto-layout-for-tableheaderview">Auto Layout for tableHeaderView</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/16471846/is-it-possible-to-use-autolayout-with-uitableviews-tableheaderview">Is it possible to use AutoLayout with UITableView&#39;s tableHeaderView?</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/19005446/table-header-view-height-is-wrong-when-using-auto-layout-ib-and-font-sizes">table header view height is wrong when using auto layout, IB, and font sizes</a></p></li>
  </ul>
  
  <p>It doesn't really make sense to change the translatesAutoresizingMaskIntoConstraints property to NO, since that just causes errors for me and doesn't make sense conceptually anyway.</p>
  
  <p>Any help would really be appreciated!</p>
  
  <p><strong>EDIT 1:</strong>
  Thanks to TomSwift's suggestion, I was able to figure it out. Instead of manually calculating the height of the overview, I can have it calculated for me as follows and then re-set the tableHeaderView as before.</p>
  
  <pre><code>[self.headerView setNeedsLayout];
  [self.headerView layoutIfNeeded];
  CGFloat height = [self.innerHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + self.innerHeaderView.frame.origin.y; // adding the origin because innerHeaderView starts partway down headerView.
  
  CGRect headerFrame = self.headerView.frame;
  headerFrame.size.height = height;
  self.headerView.frame = headerFrame;
  [self.listTableView setTableHeaderView:self.headerView];
  </code></pre>
  
  <p><strong>Edit 2:</strong> As others have noted, the solution posted in Edit 1 doesn't seem to work in viewDidLoad. It does, however, seem to work in viewWillLayoutSubviews. Example code below:</p>
  
  <pre><code>// Note 1: The variable names below don't match the variables above - this is intended to be a simplified "final" answer.
  // Note 2: _headerView was previously assigned to tableViewHeader (in loadView in my case since I now do everything programatically).
  // Note 3: autoLayout code can be setup programatically in updateViewConstraints.
  - (void)viewWillLayoutSubviews {
      [super viewWillLayoutSubviews];
  
      [_headerWrapper setNeedsLayout];
      [_headerWrapper layoutIfNeeded];
      CGFloat height = [_headerWrapper systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
  
      CGRect headerFrame = _headerWrapper.frame;
      headerFrame.size.height = height;
      _headerWrapper.frame = headerFrame;
      _tableView.tableHeaderView = _headerWrapper;
  }
  </code></pre>