Swift How to refresh UICollectionView layout after rotation of the device
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When a device rotates, a UICollectionView often needs more than a simple redraw. If cell sizes depend on the available width, you need to invalidate the layout and recompute item sizes after the new bounds are known.
Invalidate the Layout During the Size Transition
The standard hook for rotation-aware updates is viewWillTransition(to:with:). Invalidate the layout there so the collection view stops using cached geometry from the old orientation.
That tells the layout system that previously calculated attributes are no longer valid.
Recompute Item Sizes from Current Bounds
Invalidation alone is not enough if your sizing logic still uses stale width values. If you use UICollectionViewDelegateFlowLayout, compute sizes from the collection view's current bounds.
This recalculates cell size using the post-rotation width instead of a hardcoded portrait measurement.
Update the Flow Layout in viewDidLayoutSubviews
If you set itemSize directly on a UICollectionViewFlowLayout, update it in viewDidLayoutSubviews(), which runs after the new bounds are final.
This approach is often simpler than trying to keep separate cached sizes for portrait and landscape manually.
Consider Automatic Invalidation in Custom Layouts
If you use a custom layout subclass, make rotation support part of the layout itself. A common pattern is to invalidate whenever bounds size changes:
That keeps the rotation behavior close to the layout logic instead of spreading it across several controller methods.
Common Pitfalls
The biggest mistake is calling reloadData() and expecting the layout to fix itself. Reloading data refreshes cell content, but it does not replace proper layout invalidation.
Another common issue is calculating sizes too early, before the collection view has its final rotated bounds. That leaves the layout using pre-rotation measurements.
People also hardcode orientation-specific sizes instead of deriving them from the actual current width. That becomes fragile on iPad, split view, and other size changes that are not simple portrait-versus-landscape flips.
Finally, custom layouts need the same principle. If you subclass UICollectionViewLayout, invalidate cached attributes whenever bounds changes require new geometry.
Testing on iPad and split-screen sizes is useful too, because those cases often expose assumptions that were hidden on a single full-screen phone layout.
Summary
- Invalidate the collection view layout when rotation changes the available size.
- Recompute item sizes from the current collection view bounds.
- Use
viewWillTransition(to:with:)andviewDidLayoutSubviews()appropriately. - Do not rely on
reloadData()as a substitute for layout invalidation. - Derive sizes from actual width instead of hardcoded orientation assumptions.
Related reading
- Swift how to use PREPROCESSOR Flags like if DEBUG to implement API keys?
- Swift how to wrap completion into an async/await?
- Swift Images change to wrong images while scrolling after async image loading to a UITableViewCell
- Swift in keyword meaning?
- Swift in keyword meaning?
- Swift Initialize Struct with optional stored properties
- Swift ios check if remote push notifications are enabled in ios9 and ios10
- Swift JSONDecode decoding arrays fails if single element decoding fails
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.