Creating a UICollectionView programmatically
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Creating a UICollectionView programmatically gives you full control over layout, registration, and constraints without relying on Storyboards. The pattern is straightforward once you break it into parts: create a layout, create the collection view, register a cell, and implement the data source and delegate methods. Programmatic setup is especially useful in reusable components, modular UI code, and dynamic layouts.
Start with a Layout
Every collection view needs a layout object. A UICollectionViewFlowLayout is the simplest starting point.
This layout decides spacing, direction, and basic item placement.
Create the Collection View
In a view controller, instantiate the collection view with the layout and constrain it to the parent view.
That is the core setup. The collection view exists, is attached to the view hierarchy, and knows which cell class to reuse.
Build a Custom Cell
A custom cell keeps presentation logic out of the controller.
Programmatic cells are a good place to add subviews, Auto Layout constraints, and styling.
Implement the Data Source
Now provide the item count and cell configuration.
Without these methods, the collection view appears but shows no content.
Size the Items
If you use UICollectionViewFlowLayout, implement UICollectionViewDelegateFlowLayout for item sizing.
This creates a simple two-column grid with spacing already accounted for.
When Programmatic Setup Helps Most
Code-based setup is especially useful when:
- the UI is reused across modules
- layout changes depend on runtime configuration
- the project avoids Storyboards by convention
- the collection view lives inside a custom reusable view
It also makes review diffs cleaner because the behavior is visible in Swift code instead of Interface Builder metadata.
Common Pitfalls
- Forgetting to register the cell class or nib before dequeueing cells.
- Adding the collection view without disabling autoresizing masks or adding constraints.
- Implementing the collection view but forgetting to set the
dataSourceanddelegate. - Hardcoding item sizes without accounting for section insets and spacing.
- Putting too much presentation logic into the view controller instead of a custom cell.
Summary
- A programmatic
UICollectionViewstarts with a layout, a registered cell, and data source wiring. - '
UICollectionViewFlowLayoutis the easiest default layout for grids and lists.' - Custom cells keep styling and subview configuration organized.
- Auto Layout constraints are required if the collection view is created in code.
- Programmatic setup is ideal when you want precise control and reusable UI code.
Related reading
- Creating a UIImage from a UIColor to use as a background image for UIButton
- Creating and playing a sound in swift
- Creating iOS/OSX Frameworks is it necessary to codesign them before distributing to other developers?
- Creating layout constraints programmatically
- Creating NSData from NSString in Swift
- Creating temporary files in Android
- Cropping an UIImage
- Cursor not showing up in UITextView
.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.