0
votes

I have a vertical UICollectionView with standard width and variable height cells. I have an issue with the spacing. For each height of a cell the spacing after the cell is different:

Screenshot of the UICollectionView

1

1 Answers

0
votes

An UICollectionView has the delegate property. You should implement the UICollectionViewDelegateFlowLayout in some class, for example the View Controller.

This methods are responsible for spacing:

// Space around the section.
func collectionView(_ collectionView: UICollectionView, 
                   layout collectionViewLayout: UICollectionViewLayout, 
                   insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
}

// In case of vertical layout this is a vertical space between horizontal rows inside of each section
func collectionView(_ collectionView: UICollectionView, 
                    layout collectionViewLayout: UICollectionViewLayout, 
                    minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 20
}

// Horizontal space between items in row
func collectionView(_ collectionView: UICollectionView, 
                    layout collectionViewLayout: UICollectionViewLayout,  
                    minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 20
}