sizeForItemAt of UICollectionView not working in Swift

by

There was a requirement where I was setting custom size for UICollectionViewCell. Custom size wasn’t dynamic but a static tall portrait ratio size. The effective way to set your own custom size is to call implement delegate method of protocol UICollectionViewDelegateFlowLayout:


func collectionView_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return estimatedCollectionSize
}

The problem:

UICollectionView was ignoring size instruction. It was sizing the cells to fit its content only.



UICollectionView was calculating its cell’s size automatically to for the content and just plainly ignoring the implementation of delegate function.

I checked storyboard inspector and found out that estimate size property of UICollectionView was set to automatic. My solution was to set it to none.

Image solution:

You might also like: How to Send Local Video to iMessage in Swift

You might also like: AVAudioPlayer not playing any sound

You might also like: How to Get Substring With NSRange in Swift 5

You might also like: How to Fix Warning Attempt To Present UIAlertController

More Articles

Recommended posts