How to set UICollectionViewCell Width and Height programmatically
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Setting UICollectionViewCell size programmatically usually means working with a flow layout and deciding whether your cells are fixed-size or responsive to the available width. UIKit gives you two main approaches: assign a single itemSize on the layout, or implement UICollectionViewDelegateFlowLayout and calculate the size for each item.
The right choice depends on how dynamic the grid needs to be. Fixed sizes are simpler, while delegate-based sizing is better when the number of columns should adapt to screen size.
Set a Fixed Cell Size on the Layout
If every cell should have the same width and height, configure the collection view’s UICollectionViewFlowLayout.
This is the easiest option when you already know the target size. It works well for card-style layouts, photo grids with fixed thumbnails, or menu screens where every cell shares the same design.
Calculate Responsive Sizes with the Delegate
For more control, adopt UICollectionViewDelegateFlowLayout and implement collectionView(_:layout:sizeForItemAt:). This lets you compute a cell size from the collection view width, spacing, and desired number of columns.
This pattern keeps the layout consistent across different screen widths. On an iPhone in portrait, you might show two columns; on a wider screen, you could switch to three or four.
Adjust for Rotation and Size Changes
Collection view bounds can change after rotation, split-view resizing, or other layout updates. If cell sizes depend on collectionView.bounds.width, invalidate the layout when the size changes so the delegate is asked again.
That is often enough for a basic flow layout. If you want to avoid invalidating on every layout pass, you can store the previous size and invalidate only when the bounds actually change.
Choose Between Layout-Level and Delegate-Level Sizing
Use itemSize when every cell can be described with one constant size. It is simpler and easier to reason about.
Use sizeForItemAt when:
- cell width depends on screen size
- different sections need different sizes
- certain items should be larger than others
- spacing and column count are part of the calculation
For modern iOS layouts, UICollectionViewCompositionalLayout is another option, but for many apps UICollectionViewFlowLayout plus sizeForItemAt is still the most direct answer to “set width and height programmatically.”
Common Pitfalls
The most common issue is forgetting to subtract section insets and inter-item spacing when calculating width. If you divide the full screen width by the number of columns, cells will overflow or wrap unexpectedly.
Another problem is setting both itemSize and implementing sizeForItemAt without knowing which behavior you want. The delegate method gives per-item sizing, so mixing both approaches can confuse future maintenance.
Self-sizing cells are another source of conflict. If you rely on Auto Layout and estimated item sizes, manually forcing a different size can produce warnings or clipped content. Pick one sizing strategy for that screen.
Finally, do not calculate with view.frame.width too early in the lifecycle. Layout dimensions are more reliable once the collection view has its final bounds.
Summary
- Set
layout.itemSizewhen all cells share one fixed size. - Use
collectionView(_:layout:sizeForItemAt:)when the size depends on available width or item type. - Subtract insets and spacing before dividing width into columns.
- Invalidate the layout when bounds changes should trigger a recalculation.
- Avoid mixing manual sizing and self-sizing unless you understand how they interact.
Related reading
- How to set UICollectionViewDelegateFlowLayout?
- How to set UITextField height?
- How to share an image on Instagram in iOS?
- How to show a dialog to confirm that the user wishes to exit an Android Activity?
- How to show an empty view with a RecyclerView?
- How to show Done button on iOS number pad keyboard?
- How to show soft-keyboard when edittext is focused
- How to show the loading indicator in the top status bar
.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.