title: How do I create UITableView header whose height is determined by the height of its label?
body: <p>I would like to add a header to my tableView.  This header contains 1 UILabel.  The header height should be calculated based on the number of lines the label has.</p>
  
  <p>In my code, I'm adding constraints with all the edges of the label &lt;> header.  This is my attempt:</p>
  
  <pre><code>    //Add header to tableView
      header = UIView()
      header.backgroundColor = UIColor.yellowColor()
      tableView!.tableHeaderView = header
  
      //Create Label and add it to the header
      postBody = UILabel()
      postBody.text = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog."
      postBody.font = UIFont(name: "Lato-Regular", size: 16.0)
      postBody.numberOfLines = 0
      postBody.backgroundColor = FlatLime()
      header.addSubview(postBody)
  
      //Enable constraints for each item
      postBody.translatesAutoresizingMaskIntoConstraints = false
      header.translatesAutoresizingMaskIntoConstraints = false
  
      //Add constraints to the header and post body
      let postBodyLeadingConstraint = NSLayoutConstraint(item: postBody, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: header, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
      postBodyLeadingConstraint.active = true
  
      let postBodyTrailingConstraint = NSLayoutConstraint(item: postBody, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: header, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0)
      postBodyTrailingConstraint.active = true
  
  
      let postBodyTopConstraint = NSLayoutConstraint(item: postBody, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: header, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
      postBodyTopConstraint.active = true
  
  
      let postBodyBottomConstraint = NSLayoutConstraint(item: postBody, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: header, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
      postBodyBottomConstraint.active = true
  
  
      //Calculate header size
      let size = header.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
      var frame = header.frame
      frame.size.height = size.height
      header.frame = frame
      tableView!.tableHeaderView = header
      header.layoutIfNeeded()
  </code></pre>
  
  <p>This is my table:</p>
  
  <pre><code>    let nib = UINib(nibName: "MessagesTableViewCell", bundle: nil)
      let nibSimple = UINib(nibName: "SimpleMessagesTableViewCell", bundle: nil)
      self.tableView!.registerNib(nib, forCellReuseIdentifier: "MessagesTableViewCell")
      self.tableView!.registerNib(nibSimple, forCellReuseIdentifier: "SimpleMessagesTableViewCell")
      self.tableView!.dataSource = self
      self.tableView!.delegate = self
      self.tableView!.rowHeight = UITableViewAutomaticDimension
      self.tableView!.estimatedRowHeight = 100.0
      self.tableView!.separatorStyle = UITableViewCellSeparatorStyle.None
      self.tableView!.separatorColor = UIColor(hex: 0xf5f5f5)
      self.tableView!.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
      self.tableView!.clipsToBounds = true
      self.tableView!.allowsSelection = false
      self.tableView!.allowsMultipleSelection = false
      self.tableView!.keyboardDismissMode = .OnDrag
  </code></pre>
  
  <p>As you can see, the header does not take into account the height of the label (which I did numberOfLines = 0)</p>
  
  <p><a href="https://i.stack.imgur.com/q4Ohq.png" rel="noreferrer"><img src="https://i.stack.imgur.com/q4Ohq.png" alt="Header does not auto-height"></a></p>