How to center horizontally UICollectionView Cells?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To center UICollectionView cells horizontally, you usually need to center the whole row of items inside the available width, not just center the content inside each cell. The simplest solution is to calculate left and right section insets based on item width, spacing, and the current collection view width.
Center a Fixed-Width Row with Section Insets
If all items in a section have the same size, UICollectionViewDelegateFlowLayout is usually enough. Compute the total content width for the row, then return matching left and right insets.
This centers the group of cells whenever the content is narrower than the collection view.
Why sectionInset Is the Right Tool
sectionInset adds padding around the section content. That makes it a good fit when you want the items themselves to stay aligned as a centered row.
By contrast, changing the cell’s internal constraints only centers the content inside the cell, not the cells inside the collection view.
Similarly, setting contentInset changes the scrollable area, which is useful in some designs but is usually not the best first choice for centering a single row.
Handle Rotation and Size Changes
A centered layout should recalculate after the collection view width changes, such as during rotation or split-screen resizing on iPad.
If your item count or bounds change, invalidate the layout so the inset calculation runs again.
Without this, the previous inset may remain and the row can look off-center after rotation.
Dynamic Item Sizes Need a Custom Layout
If cells have self-sizing widths or the number of items per row changes dynamically, a custom UICollectionViewFlowLayout subclass is often more reliable. You can inspect layout attributes and adjust them row by row.
Here is a minimal starting point:
In more advanced cases, you override layoutAttributesForElements(in:) and shift the attributes so each row is centered based on the widths of the items in that row.
Horizontal Scrolling Is a Different Case
If the collection view scrolls horizontally and you want one item centered while scrolling, that is a different problem. In that design, you usually adjust contentInset, snapping behavior, or targetContentOffset rather than centering the whole row with section insets.
The section-inset approach in this article is best when the row should sit centered inside the available width.
Common Pitfalls
A common mistake is forgetting to include inter-item spacing when computing total row width. That makes the inset too large and the row appears slightly off-center.
Another pitfall is using frame.width too early in the view lifecycle before auto layout has given the collection view its final size.
Developers also sometimes return negative insets when the content is wider than the screen. Using max(value, 0) prevents that.
Finally, if the data source changes item count, remember that the inset formula must use the current number of visible items in the section, not a stale value.
Summary
- Centering collection view cells usually means centering the whole row with
sectionInset. - Compute total row width as cell widths plus inter-item spacing.
- Return equal left and right insets based on the collection view width.
- Invalidate the layout when bounds change so rotation keeps the row centered.
- For self-sizing or complex rows, use a custom flow layout instead of a fixed formula.

