title: How to size a UICollectionView cell to fit the contents inside it?
body: <p>I was thinking of doing this in <code>sizeForItemAt</code> but I cannot access the heights of my cell contents there. Tried doing it in <code>cellForItemAt</code> since we're creating the instance of the cell there and can reference all it's contents and their heights there, but setting the frame size of the cell within this method doesn't seem to work. Seems like one can only properly set the size in <code>sizeForItemAt</code>. Here's an example of me trying to do it in <code>cellForItemAt</code> (note that in this method I add the <code>likesCommentsLabel</code> view depending on whether it has an assigned string to its <code>.text</code> and this is what makes the height dynamic): </p>
  
  <pre><code>    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let feedCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! FeedCell
  
      feedCell.post = posts[indexPath.item] //This executes post setter and didSet in FeedCell class
  
      //Determine whether to add likesCommentsLabel
      if (feedCell.likesCommentsLabel.text != nil) {
          feedCell.addSubview(feedCell.likesCommentsLabel)
          feedCell.addConstraintsWithFormat(format: "H:|-12-[v0]|", views: feedCell.likesCommentsLabel)
          feedCell.addConstraintsWithFormat(format: "V:|-8-[v0(44)]-4-[v1]-4-[v2(200)]-8-[v3(24)]-8-[v4(0.4)][v5(44)]|", views: feedCell.profileImageView, feedCell.statusTextView, feedCell.statusImageView, feedCell.likesCommentsLabel, feedCell.dividerLineView, feedCell.credibilityButton)
  
          print("THIS RUNS")
      }
      else {
          feedCell.addConstraintsWithFormat(format: "V:|-8-[v0(44)]-4-[v1]-4-[v2(200)]-8-[v3(0.4)][v4(44)]|", views: feedCell.profileImageView, feedCell.statusTextView, feedCell.statusImageView, feedCell.dividerLineView, feedCell.credibilityButton)
          print("THIS RUNS MORE")
      }
  
      //Determine size of cell based on status text  length
      if let statusText = posts[indexPath.item].statusText {
          let rect = NSString(string: statusText).boundingRect(with: CGSize(width: view.frame.width, height: 500), options: NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin), attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14)], context: nil)
  
          var knownHeight: CGFloat = 8.0 + 44.0 + 4.0 + 4.0 + 200.0 + 8.0 + 44.0
  
          //Add to height of cell, if there's a likes/comments label
          if (feedCell.likesCommentsLabel.text != nil) {
              knownHeight = CGFloat(8.0 + 44.0 + 4.0 + 4.0 + 200.0 + 8.0 + 24.0 + 8.0 + 44.0)
          }
          else {
              knownHeight = CGFloat(8.0 + 44.0 + 4.0 + 4.0 + 200.0 + 8.0 + 44.0)
          }
          //Calculate y position based off of previous cell. If no previous cell, y = 0.
          if indexPath.item &gt; 0 {
              feedCell.frame = CGRect(x: 0, y: ((collectionView.cellForItem(at: collectionView.indexPathsForVisibleItems[indexPath.item - 1])?.frame.origin.y)! +  (collectionView.cellForItem(at: collectionView.indexPathsForVisibleItems[indexPath.item - 1])?.frame.height)!), width: view.frame.width, height: rect.height + knownHeight + 20)
          }
          else {
              feedCell.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: rect.height + knownHeight + 20)
          }
          print(feedCell.frame)
      }
  
      return feedCell
  }
  </code></pre>