How to change space between cells in UICollectionView? How to change space between cells in UICollectionView? swift swift

How to change space between cells in UICollectionView?


//Objective c- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {        return CGSizeMake((collectionView.frame.size.width/3)-20, 100);    }// Swift 2.0func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {    return CGSizeMake((collectionView.frame.size.width / 3) - 20, 100)}// Swift 3.0override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {    return CGSize(width: CGFloat((collectionView.frame.size.width / 3) - 20), height: CGFloat(100))}


Go to the storyboard , right click on collection view and enter image description here

And Check your minimum spacing between the cell and reduce it to zero .


On your code, you have to create a IBOutlet to your collection view, and then assign the collectionViewLayout as such:

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()layout.sectionInset = UIEdgeInsets(top: 20, left: 2, bottom: 10, right: 2)layout.minimumInteritemSpacing = 0layout.minimumLineSpacing = 0layout.scrollDirection = .horizontalcollectionView!.collectionViewLayout = layout