I have a UICollectionViewController that has a bunch of cells, and each cell has the screen size.
Everything works as expected if I launch the app either in portrait or in landscape.
The problem is when I change the screen orientation, and I call collectionViewLayout.invalidateLayout()
the cells size stays as it was before, without resizing them.
I don't know why this is happening.
Here's some code:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
print("size for item")
return CGSize(width: view.frame.width, height: view.frame.height)
}
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { (_) in
self.collectionViewLayout.invalidateLayout() // layout update
}, completion: nil)
}
On the first load, I get the prints from the size set, but after changing orientation, it doesn't call it, so when I change orientation the cells size doesn't fit the screen as it fits on launch.
Any ideia what maybe causing this or what am I doing wrong here?
Thank you
EDIT
Somehow I tried to insert a print statement inside the willTransition
function and I noticed it doesn't fire on screen rotation. I think the problem is there instead of the invaldateLayout()
since it never fires.
How can this override function not be automatically called?