Answer for Objective-C
and Swift2.0
: How to center align the cells of a UICollectionView?
I usually would try to conver
Use this solution for horizontal scrolling with the center align.
let totalCellWidth = Int(collectionView.frame.height * CGFloat(numberOfItems()))
// if total width is greater than collection's width, then no need to align center
if totalCellWidth > Int(collectionView.frame.width) {
return UIEdgeInsets.zero
}
let totalSpacingWidth = cellSpacing * (numberOfItems() - 1)
let leftInset = (collectionView.frame.width - CGFloat(totalCellWidth + totalSpacingWidth)) / 2
let rightInset = leftInset
return UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)