Ways to make UICollectionView cell define its size itself.

Overriding preferredLayoutAttributesFitting

When you set estimatedItemSize as above you make collection view query each cell for its actual size using this method implemetation. In the implementation you call super implementation and then mutate the attributes size as you need. Here is an example:

Using auto-layout for auto-sizing

This approach allows as not to calculate cell size at all.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store