title: How to set cell spacing and UICollectionView - UICollectionViewFlowLayout size ratio?
body: <p>I'm trying to add <code>UICollectionView</code> to <code>ViewController</code>, and I need to have 3 cells 'per row' without blank space between cells (it should look like a grid). Cell width should be one third of screen size, so I thought that the <code>layout.item</code> width should be the same. But then I get this:</p>
  
  <p><img src="https://i.stack.imgur.com/CoMTm.png" alt="layout.itemSize width = screenSize.width/3"></p>
  
  <p>If I reduce that size (by <em>7</em> or <em>8 pixels</em> e.g.), it's better, but the  third cell in row is not completely visible, and I still have that blank space (top &amp; bottom, and left &amp; right) .</p>
  
  <p><img src="https://i.stack.imgur.com/5mIIG.png" alt="enter image description here"></p>
  
  <pre><code>class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
      var collectionView: UICollectionView?
      var screenSize: CGRect!
      var screenWidth: CGFloat!
      var screenHeight: CGFloat!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          screenSize = UIScreen.mainScreen().bounds
          screenWidth = screenSize.width
          screenHeight = screenSize.height
  
          // Do any additional setup after loading the view, typically from a nib
          let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
          layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)
          layout.itemSize = CGSize(width: screenWidth / 3, height: screenWidth / 3)
          collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
          collectionView!.dataSource = self
          collectionView!.delegate = self
          collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
          collectionView!.backgroundColor = UIColor.greenColor()
          self.view.addSubview(collectionView!)
      }
  
      func numberOfSectionsInCollectionView(collectionView: UICollectionView) -&gt; Int {
          return 1
      }
  
      func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return 20
      }
  
      func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
          let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as CollectionViewCell
          cell.backgroundColor = UIColor.whiteColor()
          cell.layer.borderColor = UIColor.blackColor().CGColor
          cell.layer.borderWidth = 0.5
          cell.frame.size.width = screenWidth / 3
          cell.frame.size.height = screenWidth / 3
  
          cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
          return cell
      }
  }
  </code></pre>