title: Dynamic UICollectionView header size based on UILabel
body: <p>I've read a bunch of posts on adding header to UICollectionView. In an iOS 7+ app in Swift, I'm trying to add a header with a UILabel in it whose height should adjust based on the height of UILabel. The UILabel has lines = 0.</p>
  
  <p>I've set up the header in IB with AutoLayout</p>
  
  <p><img src="https://i.stack.imgur.com/fU10K.png" alt="enter image description here"></p>
  
  <p>The ViewController implements <code>UICollectionViewDelegate, UICollectionViewDataSource</code>. I didn't set up a custom class for the header but am using these two functions:</p>
  
  <pre><code>func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -&gt; CGSize {
        //description is a String variable defined in the class
      let size:CGSize = (description as NSString).boundingRectWithSize(CGSizeMake(CGRectGetWidth(collectionView.bounds) - 20.0, 180.0), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 16.0)], context: nil).size
      return CGSizeMake(CGRectGetWidth(collectionView.bounds), ceil(size.height))
  }
  
  func collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!) -&gt; UICollectionReusableView! {
      var reusableview:UICollectionReusableView = UICollectionReusableView()
      if (kind == UICollectionElementKindSectionHeader) {
                      //listCollectionView is an @IBOutlet UICollectionView defined at class level, using collectionView crashes
              reusableview = listCollectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "ListHeader", forIndexPath: indexPath) as UICollectionReusableView
              let label = reusableview.viewWithTag(200) as UILabel  //the UILabel within the header is tagged with 200
              label.text = description   //description is a String variable defined in the class
          }
      }
      return reusableview
  }
  </code></pre>
  
  <p>The displaying of the text seems to be working but the height calculation doesn't seem to be working (see screenshot below). Also, I don't think I can access the UILabel via the <code>collectionView...referenceSizeForHeaderInSection</code> function either. Any suggestions on how to calculate CGSize correctly?</p>
  
  <p><img src="https://i.stack.imgur.com/gK2EQ.png" alt="enter image description here"></p>