0

I have a Collection View and I want to calculate the spacing between every single cell programmatically but I can't find out how to do this.

So, how can I calculate the inter-cells spacing between UICollectionViewCells in a UICollectionView with UICollectionViewFlowLayout?

alessionossa
  • 923
  • 2
  • 15
  • 41

1 Answers1

1

Below is how you set custom cell spacing in a collectionview. You have to create an extension.

extension ViewController : UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return //whatever you want//

}

Make sure the "ViewController" that follows extension is the name of your viewcontroller.

DevKyle
  • 1,091
  • 6
  • 22
  • I use this method to set the size of the cell but how can I set the inter cell spacing with this method? – alessionossa Oct 15 '16 at 17:05
  • What I personally do is return the size of the cell based on the screen size to get desired spacing between cells. – DevKyle Oct 15 '16 at 17:11
  • So to clarify, if the screen width is 300 and you want 2 cells per row with uniform 10 spacing (screen edge to cell, cell to cell, cell to screen edge), you would return 135 for the width portion of cell size. (300 - 10 - 10 - 10) / 2 = 135. – DevKyle Oct 15 '16 at 17:26