I'm struggling with resizing collection view.
Currently I'm having a flow layout method that counts size:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if let cell = collectionView.cellForItem(at: indexPath) {
let pokemonCell = cell as! PokemonCell
pokemonCell.setNeedsDisplay()
}
return CGSize(width: 150, height: collectionView.bounds.size.height)
}
And also two methods for resizing on rotation:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
guard let flowLayout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
return
}
flowLayout.invalidateLayout()
self.collectionView.reloadData()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
collectionView.frame = self.view.bounds
self.collectionView.reloadData()
}
Cell content is centered in container. When I change layout, cell content starts jumping. Some of them reposition themselves, some remain on y position of previous layout. Sometimes content gets correct positionig and than goes back wile scrolling.