I am using an UICollectionView
with UICollectionViewFlowLayout
.
I set the size of each cell through the
collectionView:layout:sizeForItemAtIndexPath:
When switching from portrait to landscape, I would like to adjust the size of each cell to completely fit the size of the CollectionView, without leaving padding space between cells.
Questions:
1) How to change the size of a cell after a rotation event?
2) And, even better, how to make the layout where the cells always fit the entire size of the screen?
Best Answer
1) You could maintain and swap out multiple layout objects, but there's a simpler way. Just add the following to your
UICollectionViewController
subclass and adjust the sizes as required:Calling
-performBatchUpdates:completion:
will invalidate the layout and resize the cells with animation (you can just pass nil to both block params if you've no extra adjustments to perform).2) Instead of hardcoding the item sizes in
-collectionView:layout:sizeForItemAtIndexPath
, just divide the height or width of the collectionView's bounds by the number of cells you want to fit on screen. Use the height if your collectionView scrolls horizontally or the width if it scrolls vertically.