How to specify number of rows, columns and spacing for CollectionView Cell

164 Views Asked by At

I want to specify number of rows, columns and also space between collection view cell items and then i want the width and height of my cells should be determined by ios . How to do that ? Please check my following code script. When i change the number of rows and columns, sometimes i dont see equal space between cells. How to see always equal space, and also wanted rows and columns.

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

    let layout = collectionViewLayout as! UICollectionViewFlowLayout;


    layout.minimumInteritemSpacing = 0.6
    layout.minimumLineSpacing = 0.6

    let remainingInterSpace = collectionView.frame.width - (layout.minimumInteritemSpacing*(numberOfColumns-1))       
    let remainingLineSpace = collectionView.frame.height - (layout.minimumLineSpacing*(numberOfRows-1))


    let width = remainingInterSpace / numberOfColumns     
    let height = remainingLineSpace / numberOfRows


    return CGSize(width: width, height: height)

 } 
0

There are 0 best solutions below