title: Unable to set width of UICollectionViewCell with AutoLayout
body: <p>I have a Collection View with cells of dynamic height (based on a possibly multiline label and a textview inside), which adapts in height perfectly when I span it over multiple lines. However, when the text is just one word or it doesn't cover the entire screen width, the cell is just as wide as needed to be, resulting in cells next to each other instead of underneath each other.</p>
  
  <p><a href="https://i.stack.imgur.com/mD2sn.jpg" rel="nofollow noreferrer">Take a look at the result with different lengths of text inside the textview.</a></p>
  
  <p>However, I want the cells to be underneath each other, think Instagram or Twitter kind of view. I obviously need to set a width constraint for the cell so it is equal to the entire width of the screen, but I can't seem to find how to do this.</p>
  
  <p>I use FlowLayout with an estimatedItemSize in my AppDelegate:</p>
  
  <pre><code>let layout = UICollectionViewFlowLayout()
  window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))
  
  layout.estimatedItemSize = CGSize(width: 414, height: 150)
  </code></pre>
  
  <p>I also set constraints for the collectionView in viewDidLoad, so the collectionView takes on the entire width and height of the screen:</p>
  
  <pre><code>collectionView?.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
  collectionView?.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
  collectionView?.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
  collectionView?.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
  </code></pre>
  
  <p>My constraints for the label and the textview look like this, also allowing the width to take on the size required (so it's not fixed):</p>
  
  <pre><code>// vertical constraints usernameLabel &amp; commentTextView
  addConstraintsWithFormat(format: "V:|-16-[v0]-2-[v1]-16-|", views: usernameLabel, commentTextView)
  
  // horizontal constraints usernameLabel
  addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: usernameLabel)
  
  // horizontal constraints commentTextView
  addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: commentTextView)
  </code></pre>
  
  <p>All this is, according to me, essential to make this work. However, it seems to me that I have yet to set a width constraint to make this work. I have tried to add a widthAnchor on the collectionView (equal to view.widthAnchor), this didn't work:</p>
  
  <pre><code>collectionView?.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
  </code></pre>
  
  <p>I have also tried to set the width in the sizeForItemAt method, but this also didn't work:</p>
  
  <pre><code>return CGSize(width: view.frame.width, height: 100)
  </code></pre>
  
  <p>Now, I am checking the width to see where it goes wrong, but I get the result I would need, I think:</p>
  
  <pre><code>print("collectionView width: ", collectionView?.frame.size.width)
  print("collectionView height: ", collectionView?.frame.size.height)
  
  print("screen width: ", UIScreen.main.bounds.width)
  print("screen height: ", UIScreen.main.bounds.height)
  </code></pre>
  
  <p>Results in:</p>
  
  <pre><code>collectionView width:  Optional(414.0)
  collectionView height:  Optional(736.0)
  screen width:  414.0
  screen height:  736.0
  </code></pre>
  
  <p>I am out of ideas on how to move forward. All articles or posts I have gone through related to this issue either explicitly calculate the height or use the two solutions I have mentioned above.</p>
  
  <p>Any ideas on what I am doing wrong?</p>