Resizing UICollectionViewCells on rotation changes currently visible cells
<p>I have a paginated, horizontally scrolling <code>UICollectionView</code> where each cell takes up the size of the view/device screen, and one cell appears at a time.  I'm experiencing an issue where on device rotation, the cells will transition to the correct new size, but the position of the cells will be off.</p>

<p>Before Rotation:</p>

<p><a href="https://i.stack.imgur.com/pNmnt.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/pNmntm.jpg" alt="Before Rotation"></a></p>

<p>After Rotation:</p>

<p><a href="https://i.stack.imgur.com/dJVaB.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/dJVaBm.jpg" alt="After Rotation"></a></p>

<p>On rotation, how can I not only resize the collection view cells properly but ensure that the current cell stays visible?</p>

<p>I've boiled this problem down to a very simple example without a custom flow layout.</p>

<p>Setting the size of the cells to the <code>collectionView</code> frame's size (the collection view is the size of the view controller holding it):</p>

<pre><code>func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -&gt; CGSize {
    return collectionView.frame.size
}
</code></pre>

<p>Attempting to handle rotation in <code>viewWillLayoutSubviews</code>:</p>

<pre><code>override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    guard let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else { return }

    flowLayout.itemSize = collectionView.frame.size

    flowLayout.invalidateLayout()
}
</code></pre>

<p>Actually, if I call <code>invalidateLayout()</code> in <code>viewWillLayoutSubviews</code>, setting the <code>itemSize</code> here shouldn't be necessary, as <code>sizeForItemAtIndexPath</code> will be called when the layout is recreated.</p>

<p>I've also tried manually scrolling to the first visible cell during rotation after setting the new itemSize, but that shows no improvement:</p>

<pre><code>if let item = collectionView.indexPathsForVisibleItems().first {
    collectionView.scrollToItemAtIndexPath(item, atScrollPosition: .CenteredHorizontally, animated: true)
}
</code></pre>