2

I have 3 cells, first cell has label, second cell has textfield, and the last cell has button, by using this it give automatic size but how to remove space between cells

if let flowlayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            flowlayout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
        }

or how to use this method in my scenario

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

enter image description here

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
Salman500
  • 1,213
  • 1
  • 17
  • 35

5 Answers5

10

Use the following collection view delegate functions:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
    return 0 
}

And for spacing to the sides:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: minimumInteritemSpacingForSectionAtIndex UICollectionViewLayout,  section: Int) -> CGFloat {
  return 0 
}

Swift 4 & 5

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

Also check out the documentation

Elhoej
  • 741
  • 7
  • 29
6

Swift 5

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
Ahmed Safadi
  • 4,402
  • 37
  • 33
2

If you have an init function for your controller, you can avoid bulking it with the minimumLineSpacing & minimumInteritemSpacing methods and do the following:

init() {
    
    let layout = UICollectionViewFlowLayout()
    layout.minimumLineSpacing = 0
    layout.minimumInteritemSpacing = 0
    
    super.init(collectionViewLayout: layout)
}
1

See: https://developer.apple.com/documentation/uikit/uicollectionviewflowlayout

let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.minimumInteritemSpacing = 0 // The minimum spacing to use between items in the same row.
myCollectionView.layout = layout // or myCollectionView.collectionViewLayout ( I don't have compiler here so I do not know the property name, but I know it has property that has layout in its name )
J. Doe
  • 12,159
  • 9
  • 60
  • 114
  • @Salman500 If the item size holds the same size as the intristic content size of the cells, my answer should work. Make sure the cells size are equal to the intristic content size – J. Doe Jun 11 '18 at 11:29
0

Swift 4.0

There are 2 delegate method of UICollectionViewDelegateFlowLayout to remove space between item and section.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40