UICollectionView Self Sizing Cells with Auto Layout
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Self-sizing collection-view cells let the cell height or width follow the content instead of a hardcoded size. The feature works well with Auto Layout, but only if the collection view layout, the cell constraints, and the content’s intrinsic size all agree on how the cell should be measured.
Enable Self-Sizing in the Layout
For a UICollectionViewFlowLayout, the usual starting point is to give the layout an estimated item size.
That tells the flow layout to ask Auto Layout for the final size instead of requiring you to return a fixed item size from the delegate.
Build the Cell with Complete Constraints
The cell must be fully constrained from edge to edge. If any subview is missing a top, bottom, leading, or trailing path to the contentView, the layout engine cannot infer the correct size.
The label has a clear padded box, so Auto Layout can compute the cell’s content size.
Register and Use the Cell Normally
Once the layout and cell are configured, the collection view setup remains standard.
Multi-Line Labels Need a Real Width Constraint
A common trap is using a label with numberOfLines = 0 but never giving Auto Layout enough information about the final width. The cell can only derive a height from wrapped text after it knows how wide the text area is meant to be.
For full-width list-style cells, the flow layout’s available width plus your insets usually defines that naturally. For more complex layouts, you may need a custom layout or sizing override.
When preferredLayoutAttributesFitting Helps
If a cell still sizes incorrectly, override preferredLayoutAttributesFitting(_:) and force a layout pass before measuring. That is often helpful for custom content or older layout behavior.
Common Pitfalls
The biggest mistake is incomplete constraints inside the cell. Another is leaving estimatedItemSize unset, which prevents the flow layout from asking Auto Layout for dynamic sizing. Developers also often expect multi-line labels to self-size without a meaningful width context. Finally, mixing manual size delegate methods with self-sizing behavior can produce confusing results because the layout receives conflicting instructions.
Summary
- Set
estimatedItemSizeon the collection-view flow layout. - Fully constrain the cell’s content to
contentViewon all sides. - Make sure multi-line content has a clear width to wrap within.
- Register and use the cell normally; self-sizing happens through Auto Layout.
- If sizing still looks wrong, inspect constraints first before adding manual measurement code.
Related reading
- UICollectionView Set number of columns
- UICollectionView spacing margins
- UICollectionView's cellForItemAtIndexPath is not being called
- UIDevice uniqueIdentifier deprecated - What to do now?
- UIFont - how to get system thin font
- UIGestureRecognizer blocks subview for handling touch events
- UIGestureRecognizer on UIImageView
- UIImage - implementing an auto levels algorithm
.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.