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

后端 未结 11 2056
不知归路
不知归路 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:46

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    
    let allCellWidth = KcellWidth * numberOfCell
    let cellSpacingWidth = CellSpacing * (numberOfCell - 1)
    
    let leftInset = (collectionViewWidth - CGFloat(allCellWidth + cellSpacingWidth)) / 2
    let rightInset = leftInset
    
    return UIEdgeInsetsMake(0, leftInset, 0, rightInset)
    }
    

提交回复
热议问题