How to set UICollectionViewDelegateFlowLayout?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UICollectionViewDelegateFlowLayout lets your code control item size, spacing, and section insets for a flow-layout collection view at runtime. It is the usual choice when the layout depends on screen width, orientation, or section-specific rules and you want more control than the default storyboard settings provide.
Wire the Delegate Correctly
The first requirement is simple: the collection view's delegate must actually point to an object that conforms to UICollectionViewDelegateFlowLayout.
If the delegate is not assigned, the layout callbacks are never called, which makes every sizing bug look mysterious even though the setup is the real problem.
Implement sizeForItemAt
The method most people need first is collectionView(_:layout:sizeForItemAt:). The common pattern is to compute a width from the collection view bounds, section insets, and inter-item spacing.
The important part is subtracting all spacing before dividing. If you divide too early, cells will not fit cleanly across the row.
Configure Insets and Spacing
UICollectionViewDelegateFlowLayout also lets you define section padding and row spacing dynamically.
These methods are useful even when the overall layout is simple, because they keep the spacing logic close to the sizing logic.
Handle Rotation and Size Changes
Collection views often break when the device rotates or the app enters split-screen mode because the item size was calculated once and then never refreshed. If the layout depends on width, invalidate it when the bounds change.
That tells the flow layout to ask for item sizes again with the new dimensions.
Vary Layout by Section or Item
A major benefit of the delegate approach is that it can return different sizes or insets based on section or index path. For example, a featured first section can use a full-width card while the rest of the collection uses a grid.
That kind of rule is exactly what makes the delegate protocol useful.
Know When Flow Layout Is No Longer the Right Tool
Flow layout is excellent for grids and row-based arrangements. If the screen starts looking like a magazine layout with nested groups, orthogonal scrolling sections, or highly irregular item placement, compositional layout may express the design more clearly.
The engineering rule is not to use the fanciest layout API. It is to use the simplest one that still matches the UI honestly.
Common Pitfalls
- Forgetting to set the collection view delegate, so layout methods never run.
- Calculating cell width without subtracting all insets and spacing first.
- Hardcoding sizes that break on rotation or split-screen changes.
- Returning spacing values in code that conflict with assumptions from Interface Builder.
- Forcing a complex custom design into flow-layout math when a different layout system would be clearer.
Summary
- Conform to
UICollectionViewDelegateFlowLayoutand assign the collection view delegate. - Implement
sizeForItemAtwhen item size depends on runtime dimensions. - Use the inset and spacing delegate methods so layout math stays explicit.
- Invalidate the layout when the collection view width changes.
- Use flow layout for grids and rows, and switch tools only when the design has clearly outgrown it.
Related reading
- 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
- How to silence a warning in Swift?
.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.