I had the same problem, the cell was looking good on iOS 11, 12, 13, but on iOS 14 simulator somehow the preferredLayoutAttributesFitting
returned with the same height for every cell, ignoring that some cell needs more height.
The problematic code on iOS 14 (working on iOS 13 and below):
override func preferredLayoutAttributesFitting(
_ layoutAttributes: UICollectionViewLayoutAttributes
) -> UICollectionViewLayoutAttributes {
layoutAttributes.bounds.size.height = systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
return layoutAttributes
}
However there is a contentView
property that you can pass to this function (if your cell is subclass of UICollectionViewCell
you already have that)
Solution (working on all iOS version I've tried):
override func preferredLayoutAttributesFitting(
_ layoutAttributes: UICollectionViewLayoutAttributes
) -> UICollectionViewLayoutAttributes {
layoutAttributes.bounds.size.height = contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
return layoutAttributes
}
and voila it works again.
I would appreciate if someone could explain what is happening here in the background, what has changed in iOS 14...
I'm not saying this is the solution for all iOS 14 self-sizing cell issue but it's worth a try! :)