title: How to auto-resize UICollectionView when device rotates?
body: <p>I'm trying to resize my collectionViewCells for every time i rotate device but i cant figure it out how. Here is my problem and my code. It's in Swift 3 and Xcode 8.</p>
  
  <p>It's the first screen that we load
  <a href="https://i.stack.imgur.com/tmdUd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tmdUd.png" alt="1"></a></p>
  
  <p>And when i rotate device cell's still has same width-height
  <a href="https://i.stack.imgur.com/YolQB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YolQB.png" alt="2"></a></p>
  
  <p>On the other hand, if i load firstScreen(HomePage) when device is rotated (Landscape) and then i rotate device again to Portrait, the cells overflows so i cant see sides of them. Because width is still the same with Landscape width.</p>
  
  <pre><code>    // MARK: UICollectionViewDataSource
  
  override func numberOfSections(in collectionView: UICollectionView) -&gt; Int {
      return 1
  }
  
  override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return allNews.count
  }
  
  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! HomeCollectionViewCell
  
      // Configure the cell
      cell.imageView.image = allNews[indexPath.row].newsPic
      cell.textView.text = allNews[indexPath.row].newsTitle
      return cell
  }
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
      let height = (view.frame.width - 32) * 9 / 16
      return CGSize(width: view.frame.width, height: height + 94)
  }
  
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -&gt; CGFloat {
      return 0
  }
  
  
  // MARK: Orientation Changes ( reload the CollectionLayout )
  override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
  
  }
  </code></pre>
  
  <p>Last of all i didn't used Auto-Layout in Main.storyboard. Here is my setupViews() method in HomeCollectionViewCell class</p>
  
  <pre><code>    private func setupViews() {
      addSubview(imageView)
      addSubview(textView)
      addSubview(separatorView)
      addSubview(dateView)
  
      addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: imageView)
      addConstraintsWithFormat(format: "V:|-16-[v0]-8-[v1(40)]-2-[v2(20)]-8-|", views: imageView, textView, dateView)
  
      addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: textView)
      addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: dateView)
  
      addConstraintsWithFormat(format: "H:|[v0]|", views: separatorView)
      addConstraintsWithFormat(format: "V:[v0(1)]|", views: separatorView)
  }
  </code></pre>
  
  <p>Regards.</p>
  
  <p><strong>SOLVED!:</strong> I used this method instead of override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {}</p>
  
  <pre><code>override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
      super.viewWillTransition(to: size, with: coordinator)
      collectionView?.collectionViewLayout.invalidateLayout()
  }
  </code></pre>