Cell spacing in UICollectionView
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In UICollectionView, “cell spacing” usually means two different layout controls: the spacing between items on the same row and the spacing between rows themselves. If you are using UICollectionViewFlowLayout, both are configurable and often adjusted together with section insets and item size.
The main challenge is that spacing is not independent of cell width. If your item width calculation ignores spacing and insets, the layout will wrap awkwardly or leave inconsistent gaps.
The Three Main Spacing Controls
With flow layout, the most relevant properties are:
- '
minimumInteritemSpacingfor spacing between items on the same line' - '
minimumLineSpacingfor spacing between rows' - '
sectionInsetfor padding around the whole section'
Here is a direct configuration example.
That gives you a basic grid with predictable spacing rules.
Use the Flow Layout Delegate for Dynamic Values
If spacing depends on screen size or section, implement UICollectionViewDelegateFlowLayout.
This is the usual approach when spacing needs to vary by section or environment.
Spacing Depends on Item Size Too
A frequent source of layout bugs is calculating cell width without subtracting insets and inter-item spacing first.
For two columns with 16-point horizontal insets and 8-point spacing between cells:
If you skip this calculation and just divide the full width by two, your spacing rules will appear to “not work” because the cells already consumed the available space.
Section Insets Versus Cell Spacing
Developers often mix these up.
- section inset controls the outer margin around the section
- inter-item spacing controls gaps between neighboring cells
- line spacing controls vertical gaps between rows
If the first cell is too close to the edge, change the inset, not the inter-item spacing.
Modern Layouts Can Use Compositional Layout
If you are building a newer layout with UICollectionViewCompositionalLayout, spacing is configured differently through groups and item content insets. That model is often better for complex screens, but the same design principle applies: decide where outer padding ends and item-to-item spacing begins.
For simple grids, flow layout is still easier to reason about.
Common Pitfalls
A common mistake is setting spacing correctly but forgetting to account for it when calculating item size. The result is unexpected wrapping or compressed cells.
Another mistake is trying to use line spacing to create outer margins. That is what section insets are for.
Developers also sometimes expect the minimum spacing values to guarantee exact spacing in every situation. Flow layout may adjust placement slightly depending on available space.
Finally, if the layout seems inconsistent, check whether self-sizing cells or estimated sizes are influencing the final geometry.
Summary
- In flow layout, cell spacing is mainly controlled by inter-item spacing, line spacing, and section insets.
- Item size and spacing must be calculated together, not independently.
- Use
UICollectionViewDelegateFlowLayoutwhen spacing should be dynamic. - Section insets handle outer margins; inter-item spacing handles gaps between cells.
- Most spacing bugs are really sizing bugs in disguise.

