title: Using autolayout in a tableHeaderView
body: <p>I have a <code>UIView</code> subclass that contains a multi-line <code>UILabel</code>. This view uses autolayout.</p>
  
  <p><img src="https://i.stack.imgur.com/Gm7Wu.png" alt="enter image description here"></p>
  
  <p>I would like to set this view as the <code>tableHeaderView</code> of a <code>UITableView</code> (<em>not</em> a section header). The height of this header will depend on the text of the label, which in turn depends on the width of the device. The sort of scenario autolayout should be great at.</p>
  
  <p>I have found and attempted <a href="https://stackoverflow.com/questions/12610783/auto-layout-still-required-after-executing-layoutsubviews-with-uitableviewcel">many</a> <a href="https://stackoverflow.com/questions/16471846/is-it-possible-to-use-autolayout-with-uitableviews-tableheaderview">many</a> <a href="https://stackoverflow.com/questions/18118021/how-to-resize-superview-to-fit-all-subviews-with-autolayout">solutions</a> to get this working, but to no avail. Some of the things I've tried:</p>
  
  <ul>
  <li>setting a <code>preferredMaxLayoutWidth</code> on each label during <code>layoutSubviews</code></li>
  <li>defining an <code>intrinsicContentSize</code></li>
  <li>attempting to figure out the required size for the view and setting the <code>tableHeaderView</code>'s frame manually.</li>
  <li>adding a width constraint to the view when the header is set</li>
  <li>a bunch of other things</li>
  </ul>
  
  <p>Some of the various failures I've encountered:</p>
  
  <ul>
  <li>label extends beyond the width of the view, doesn't wrap</li>
  <li>frame's height is 0</li>
  <li>app crashes with exception <code>Auto Layout still required after executing -layoutSubviews</code></li>
  </ul>
  
  <p>The solution (or solutions, if necessary) should work for both iOS 7 and iOS 8. Note that all of this is being done programmatically. I've set up a <a href="http://benpackard.org/SO/AutoLayoutHeaderTest.zip" rel="noreferrer">small sample project</a> in case you want to hack on it to see the issue. I've reset my efforts to the following start point:</p>
  
  <pre><code>SCAMessageView *header = [[SCAMessageView alloc] init];
  header.titleLabel.text = @"Warning";
  header.subtitleLabel.text = @"This is a message with enough text to span multiple lines. This text is set at runtime and might be short or long.";
  self.tableView.tableHeaderView = header;
  </code></pre>
  
  <p>What am I missing?</p>