I have a UICollectionView
with 7 items per row (or actually, the width of each item is the collectionView.bounds.width
divided by 7).
Here is a
You have to somehow distribute the additional pixels. Don't worry that all cells won't have exactly the same width, 1px difference won't be visible:
let numColumns = 7
let availableWidth = collectionView.bounds.size.width
let minWidth = floor(availableWidth / CGFloat(numColumns))
let remainder = availableWidth - minWidth * CGFloat(numColumns)
Now what to do with the remainder? Let's just add pixels to cell from the left:
let columnIndex = indexPath.row % numColumns
let cellWidth = CGFloat(columnIndex) < remainder ? minWidth + 1 : minWidth
return CGSize(width: cellWidth, height: 45)
This is just the basic idea.