2

I have a 7 cells UICollectionView displayed like this

X X
X X
X X
X

I need it to be displayed like this

X X
X X
X X
 X

How's this can be done in Swift?

EnasAZ
  • 82
  • 6

2 Answers2

0

you can do this simple calculation in your collectionViewLayout function.

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

    //return itemSize
    let spaceBetweenCell :CGFloat = 0
    let screenWidth = UIScreen.main.bounds.size.width - CGFloat(2 * spaceBetweenCell)
    let totalSpace = spaceBetweenCell * 1.0
    if indexPath.row == categories.count-1 {
      // check if last cell is odd
      if categories.count % 2  == 1 {
        return CGSize(width: screenWidth , height: (screenWidth-totalSpace)/4) // all Width and  same previous height
      }else {
        return itemSize
      }
    }else{
      return itemSize
    }
  }
Marwen Doukh
  • 1,946
  • 17
  • 26
-1

Here is my code just i add datasource count to 7 items

Also make sure that you create your cell

 let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
 "cell", for: indexPath) as! UICollectionViewCell

, Spacing between items =4

Code

extension ViewController :UICollectionViewDelegate , UICollectionViewDelegateFlowLayout,UICollectionViewDataSource{
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 7 // data source
      }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! UICollectionViewCell
           cell.backgroundColor = .red
          return cell
       }

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

        let spaceBetweenCell :CGFloat = 4.0
        let screenWidth = UIScreen.main.bounds.size.width - CGFloat(2 * spaceBetweenCell)
        let totalSpace = spaceBetweenCell * 1.0;  // there is one space in 2 item
        if indexPath.row == 6 { // indexpath.row  == datasource.count - 1
             if 6 % 2  == 1{  // check if last cell is odd
                return CGSize(width: screenWidth , height: (screenWidth-totalSpace)/2) // all Width and  same previous height
             }else{
                return CGSize(width: (screenWidth - totalSpace)/2, height: (screenWidth-totalSpace)/2)

            }
        }else{
            return CGSize(width: (screenWidth - totalSpace)/2, height: (screenWidth-totalSpace)/2)

        }

    }

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

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



}
Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35
  • Your answer will work for hardcoded items only. For example what if I have different devices with different item count in a row? – Gargo Mar 03 '23 at 13:23