用于分页UICollectionView的居中单元格

时间:2017-04-11 05:45:13

标签: ios swift uicollectionview

我有一个像这样的UICollectionView设置:

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

我的代表们:

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

}

当我运行应用程序时,它看起来像这样:

enter image description here

我的问题是,如何让细胞居中?我注意到我可以调整单元格之间的间距,但是如何在红色单元格的左边添加间距?对此的任何指示都将非常感激。谢谢!

3 个答案:

答案 0 :(得分:3)

尝试此委托功能将有所帮助,您需要根据需要设置单元格的宽度

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

希望它能帮到你

答案 1 :(得分:0)

您尝试的代码可以帮助您..

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

答案 2 :(得分:0)

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

我希望它对你有用