1

Using UICollectionView flow delegate, I'm trying to accomplish this but I still need to remove the huge spacing in the two cell areas.

enter image description here

My Code :

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if indexPath.row == 0  || indexPath.row == 5 {
        return CGSize(width: (getWidth()/2) , height:  (getWidth()/2))
    }
    return CGSize(width: (getWidth()/4) , height: (getWidth()/4))
}

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

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}

Expected :

enter image description here

Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
a.masri
  • 2,439
  • 1
  • 14
  • 32

1 Answers1

2

It is impossible to do this using plain UICollectionViewLayout. By its nature, UICollectionViewLayout is a "line-braking-layout" and line-height is the height of its tallest element. Please see the following:

Apple docs on using UICollectionViewFlowLayout

However, the behavior you want can be achieved by subclassing UICollectionViewFlowLayout. Here is a guide on how to do it:

Creating custom layouts

Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
fewlinesofcode
  • 3,007
  • 1
  • 13
  • 30