Apr 10, 2021

2 min read

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.