I have the same issue and this is how I can fix it.
But remember to choose Collection View -> Size Inspector -> Estimate Size set to None.
This will let you conform to UICollectionViewDelegateFlowLayout.
extension BaseViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 8
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 8
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let itemWidth = ((collectionView.bounds.width - 16)/3)
return CGSize(width: itemWidth, height: itemWidth * 235/125)
}
}
If you have 3 items in a row, the spacing between items is 8. The width of each item is calculated as: Width = (CollectionView.Width - 16) / 3. For 5 items, the formula becomes: Width = (CollectionView.Width - 32) / 5.
235/125 represents the ratio of your cell:
let itemWidth = ((collectionView.bounds.width - 16)/3)
return CGSize(width: itemWidth, height: itemWidth * 235/125)