0

In my app I am using using UICollectionView. I want to set fix space between UICollectionViewCell. so how can I do this?

here is my code:

 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 20

}

by this line I can set space between cell?

here is my screenshot please see this. and let me know how can i set fix distance in both landscape or portrait mode

enter image description here enter image description here

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
Jack.Right
  • 974
  • 3
  • 14
  • 30
  • 1
    Possible duplicate of [Cell spacing in UICollectionView](http://stackoverflow.com/questions/17229350/cell-spacing-in-uicollectionview) – Bhavin Bhadani Jun 07 '16 at 09:56

4 Answers4

1

Maybe,you need implementation follow method.

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
酷酷的哀殿
  • 1,021
  • 6
  • 18
1

You can adjust the spacing between UICollectionCell by using the storyboard Min. spacing property of UICollectionView.

enter image description here

Here you have to set Min spacing value for cells and lines.

Hope it would help you.

Bhumika
  • 876
  • 7
  • 20
0

You can also manage the spacing using the size inspector of Collection View

https://i.stack.imgur.com/Nvp3g.png

Avim
  • 101
  • 1
  • 10
0

If you want exact spacing between the cells you need to calculate the size for the cells that will best fit to allow for the spacing you need without wrapping while considering the sectionInsets and CollectionView bounds. eg.

let desiredSpacing: CGFloat = 20.0

if let layout = self.collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
    layout.minimumLineSpacing = desiredSpacing
    let totalCellsContentWidth = self.collectionView!.bounds.width - layout.sectionInset.left - layout.sectionInset.right
    let numberOfCellsPerRow: CGFloat = 10
    let numberOfSpacesPerRow = numberOfCellsPerRow - 1
    let cellWidth = (totalCellsContentWidth - (numberOfSpacesPerRow * desiredSpacing) / numberOfCellsPerRow)
    layout.itemSize = CGSize(width: cellWidth, height: cellWidth)
}

Assuming your cells are the same size the minimumLineSpacing is simple but would otherwise expand to fit the largest cell on the row before wrapping to the next line. As you can see it's the cell spacing that is a bit more complicated.

Dallas Johnson
  • 1,546
  • 10
  • 13