EnasAZ
EnasAZ

Reputation: 82

Center the last row in UICollectionView

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?

Upvotes: 2

Views: 1980

Answers (2)

Ankit Panchotiya
Ankit Panchotiya

Reputation: 351

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
    }
  }

Upvotes: 0

Abdelahad Darwish
Abdelahad Darwish

Reputation: 6067

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
    }



}

Upvotes: -1

Related Questions