79253252

Date: 2024-12-05 02:38:37
Score: 4
Natty:
Report link

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)
Reasons:
  • Blacklisted phrase (1): I have the same issue
  • Blacklisted phrase (1): this link
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): I have the same issue
  • Low reputation (1):
Posted by: MinhNghien26