title: How can I have image views in a collection view resize themselves depending on the screen size?
body: <p>I have a collection view of image views, and when run in the simulator on an iPhone 7, the layout is how I want it - images displayed 3 per line, with no spacing in between. However when I run it on my phone (iPhone 6+), there is spacing between the images.</p>
  
  <p>These are the functions that set up the layout:</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -&gt; UIEdgeInsets {
      return UIEdgeInsetsMake(0,0,0,0)
  }
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -&gt; CGFloat {
      return 0
  }
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -&gt; CGFloat {
      return 0
  }
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
  
      let imgWidth = view.bounds.width/3.0
      let imgHeight = imgWidth
  
      return CGSize(width: imgWidth, height: imgHeight)
  }
  </code></pre>
  
  <p>I was thinking that <code>sizeForItem</code> would re-scale the images by stating that the width would be the screen width divided by three, and the height equal to that. But I think I'm confused about that and perhaps it's only resizing the cell, and not the image view within. However I'm not sure where I can set the image view size to scale dynamically with the screen size.</p>
  
  <p>How can I change it so the image cells in the collection view resize themselves accordingly, and remain 3 per line with no spacing, regardless of screen size?</p>
  
  <p>EDIT: After putting <code>cell.postImage.contentMode = UIViewContentMode.scaleAspectFill</code> into <code>cellForItem</code> method:</p>
  
  <p><a href="https://i.stack.imgur.com/rgkHM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rgkHM.png" alt="enter image description here"></a></p>
  
  <p>EDIT 2: After trying iOSFreak's solution, problem still persists:</p>
  
  <p><a href="https://i.stack.imgur.com/NQU3K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NQU3K.png" alt="enter image description here"></a></p>