Centering paging cells for a UICollectionView

I have a UICollectionView setting, for example:

In viewDidLoad:

func setupCollectionView() {
  collectionView.delegate = self
  collectionView.dataSource = self
  collectionView.register(UINib(nibName: "NewQuizzesCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "NewQuizzesCollectionViewCell")

  let flowLayout = UICollectionViewFlowLayout()
  flowLayout.scrollDirection = .horizontal
  flowLayout.minimumInteritemSpacing = 20
  flowLayout.minimumLineSpacing = 20
  collectionView.isPagingEnabled = true
  collectionView.setCollectionViewLayout(flowLayout, animated: false)
}

And my delegates:

extension NewQuizzesViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: NewQuizzesCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewQuizzesCollectionViewCell", for: indexPath) as! NewQuizzesCollectionViewCell
        cell.backgroundColor = colors[indexPath.row]
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.size.width*0.8, height: collectionView.frame.size.height*0.8)
    }

}

When I run the application, it looks like this:

enter image description here

My question is: how can I make the cells centered? I noticed that I can adjust the spacing between cells, but how would I add a gap to the left of the red cell? Any pointers to this would be really appreciated. Thank!

+4
source share
3 answers

Try using this delegate function and you need to set the cell width as desired.

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

Hope this helps you.

+3
source

for swift 3.0

minimumLineSpacingForSectionAt UICollectionView.

UICollectionViewDelegateFlowLayout, minimumLineSpacingForSectionAt,

class HomeBestSallingCatgCell: DatasourceCell ,UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
   .......
   ......

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
      return 0 //set return 0 for no spacing you can check to change the return value
    }
 }

, .

+1

You are trying to use this code that may help you.

 override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

    let offSet = self.collectionView.contentOffset
    let width = self.collectionView.bounds.size.width

    let index = round(offSet.x / width)
    let newPoint = CGPoint(x: index * size.width, y: offSet.y)


    coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) in

    },completion: {(UIVIewTransitionCoordinatorContext) in
        self.collectionView.reloadData()
        self.collectionView.setContentOffset(newPoint, animated: true)
    })

}


 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.height)
}
0
source

Source: https://habr.com/ru/post/1674541/


All Articles