How to center align the cells of a UICollectionView in Swift3.0?

后端 未结 11 2099
不知归路
不知归路 2021-02-14 11:25

Description:

Answer for Objective-C and Swift2.0: How to center align the cells of a UICollectionView?

I usually would try to conver

11条回答
  •  悲哀的现实
    2021-02-14 11:33

    Assuming that you are conforming to UICollectionViewDelegateFlowLayout, it should be:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        let edgeInsets = (screenWight - (CGFloat(elements.count) * 50) - (CGFloat(elements.count) * 10)) / 2
        return UIEdgeInsets(top: 0, left: edgeInsets, bottom: 0, right: 0)
    }
    

提交回复
热议问题