How to center align the cells of a UICollectionView?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
UICollectionViewFlowLayout lays out items from left to right by default, so rows usually hug the leading edge of the container. If you want each row of cells centered horizontally, you need to adjust the layout attributes after the flow layout computes them.
Why The Default Flow Layout Is Not Enough
The built-in flow layout knows item size, spacing, and section insets, but it does not automatically recenter a partially filled row. If a row contains only two narrow items, they still start at the left inset rather than being centered in the available width.
That is why most solutions subclass UICollectionViewFlowLayout and rewrite the x positions of items row by row.
A Custom Centering Flow Layout
This groups visible cells by row, calculates the row width, and shifts the row horizontally so it sits in the center.
Using The Layout
If your cells are fixed width, that may be all you need.
Self-Sizing Cells Need Extra Care
If the cell width comes from Auto Layout, use estimated sizing so the flow layout knows the final widths before centering.
With self-sizing cells, layout invalidation matters. The collection view may need to re-layout after content changes so the centering calculation uses final item widths instead of outdated estimates.
Section Insets And Spacing Still Matter
Centering a row does not replace section insets. Insets still define the safe drawing margins, while the centering logic works inside that available width.
A common mistake is to set left and right insets to zero and then wonder why the row feels too close to the edges. The centering algorithm will honor whatever insets you configure.
Compositional Layout Is Different
If your design is built with UICollectionViewCompositionalLayout, you often solve horizontal alignment at the group level instead. But for classic flow-layout grids with dynamic row content, a custom UICollectionViewFlowLayout subclass is still a very practical solution.
Invalidate On Bounds Changes
Centering can break after rotation if the layout is not recalculated for the new width.
Adding that override to the layout class helps keep alignment correct when the collection view width changes.
Common Pitfalls
The most common mistake is modifying the original layout attributes from super instead of working on copies, which can produce warnings or unstable layout behavior. Another is grouping rows by exact floating-point equality on minY; a small tolerance is safer. Developers also often forget that centering should ignore headers and supplementary views, so the logic should filter for .cell attributes only. Finally, if the collection view width changes after rotation, the rows must be invalidated and recomputed.
Summary
- Default flow layout left-aligns rows, even when the last row is short.
- Centering rows usually requires a custom
UICollectionViewFlowLayoutsubclass. - Group cells by row, compute row width, and shift the row horizontally.
- Self-sizing cells and rotation require correct invalidation behavior.
- Filter to cell attributes only so supplementary views are not repositioned incorrectly.

