iosswiftuicollectionviewcellspacing

How to set custom Collection View Cell spacing for every cell


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?


Solution

  • 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.