Keeping the contentOffset in a UICollectionView while rotating Interface Orientation
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Keeping a UICollectionView at the same visual position during rotation is harder than just saving and restoring contentOffset. The reason is that rotation changes the collection view's bounds and often its layout, so the old raw offset may no longer correspond to the same item after the new geometry is applied.
Core Sections
Why contentOffset jumps during rotation
When the interface rotates, UIKit changes the collection view's size. A flow layout may recompute item widths, spacing, section insets, and line breaks. That means an offset that pointed at the middle of one item in portrait may point somewhere else in landscape.
So the real goal is usually not "restore the exact numeric offset." The real goal is "keep the same content visible."
Save a stable reference before rotation
A reliable approach is to identify the visible item closest to the center before the transition starts.
This usually works better than restoring a raw point because it preserves semantic position, not just a number.
When saving raw contentOffset is good enough
If your layout does not change meaningfully across orientations, you can save and restore the offset directly.
This is simpler, but it works best only when cell sizes and layout math remain stable after rotation.
Invalidate layout before restoring position
If the collection view uses UICollectionViewFlowLayout or a custom layout that depends on bounds, make sure the layout has updated before you restore position. Otherwise you may restore against stale geometry and get another jump immediately after.
That is why the completion block of the rotation coordinator is often the right place to restore position.
Choose the right anchor for your UI
Depending on the design, you may want to preserve:
- the centered item
- the top-left visible item
- a selected item
- the approximate scroll progress
A horizontally paging interface usually cares about the centered item. A vertically scrolling grid may care more about the first visible item. The correct anchor depends on what the user perceives as "staying in place."
Common Pitfalls
- Saving and restoring raw
contentOffseteven though the layout geometry changes across orientation. - Restoring the scroll position before the collection view layout has recalculated for the new bounds.
- Preserving a pixel offset when the real requirement is to preserve the visible item.
- Forgetting that content insets, safe areas, and section spacing can change during rotation.
- Testing only one layout mode and then discovering jumps in grids, paging layouts, or self-sizing cells.
Summary
- Rotation changes collection view bounds and often changes layout geometry too.
- Restoring
contentOffsetdirectly is only reliable when the layout stays effectively the same. - In many cases, preserving an index path is better than preserving a raw point.
- Restore position after layout invalidation, usually in the transition completion block.
- Decide whether your UI should preserve an item, a page, or a numeric offset, then implement the matching strategy.

