UICollectionViewCell content wrong size on first load
For those using Storyboard + AutoLayout, and choosing which constraints are active at runtime:
When using dequeueReusableCell, on first load, the cell is created but its view has not been initialised, so the constraint changes aren't saved - whatever you have set in Storyboard is used. Solution for me was to update the constraints after the view has loaded:
override func layoutSubviews() {
super.layoutSubviews()
adjustIconWidths()
}
I had once similar problem (content of my cell was not fitting cell even with correct autolayout). The bug was caused by not calling
super.layoutSubviews()
in overriden function layoutSubviews() that was in Cell's class.
What worked for me was changing Estimate Size
on the Collection View from Automatic to None.
It looks like it still retains it's 100x100px basic size for the contentView while the cell itself gets the right size already. You can force the layout to reset adding the following line just before you return the cell:
cell?.layoutIfNeeded()