UICollectionView must be initialized with a non-nil layout parameter
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
The crash UICollectionView must be initialized with a non-nil layout parameter occurs when you create a UICollectionView without providing a UICollectionViewLayout object. Unlike UITableView, a collection view requires a layout at init time because it needs to know how to position cells before displaying anything. This commonly happens when using init(frame:) instead of init(frame:collectionViewLayout:), when loading from a storyboard with a missing layout, or when a programmatic initializer passes nil.
The Crash
UICollectionView does not have an init(frame:) initializer. The only designated initializer is init(frame:collectionViewLayout:).
Fix 1: Programmatic Initialization (Most Common)
Always pass a layout object. UICollectionViewFlowLayout is the standard grid layout provided by UIKit.
Fix 2: In a UIViewController
Fix 3: UICollectionViewController
UICollectionViewController requires a layout in its initializer:
Fix 4: Storyboard / Interface Builder
When using storyboards, the collection view layout is set in Interface Builder:
- Select the UICollectionView in the storyboard
- In the Attributes Inspector, verify "Layout" is set to "Flow" (not "Custom" with a nil class)
- If using a custom layout, ensure the class name is correct in the Identity Inspector
If the layout class cannot be found at runtime (typo, missing module), the collection view receives nil and crashes.
Fix 5: Loading from Nib with init(coder:)
Changing Layout After Initialization
You can change the layout at any time after initialization:
Compositional Layout (iOS 13+)
Modern iOS uses UICollectionViewCompositionalLayout for complex layouts:
Custom Layout
Common Pitfalls
- Using
init(frame:)orinit():UICollectionViewonly hasinit(frame:collectionViewLayout:)as its designated initializer. Calling any other init variant crashes. UICollectionViewControllerwithout layout: PushingMyCollectionVC()without a layout crashes. Always useMyCollectionVC(collectionViewLayout: layout)or overrideinit()to provide a default.- Custom layout class not found in storyboard: If the layout class name in Interface Builder has a typo or the module is wrong, the layout resolves to
nilat runtime. - Setting
collectionViewLayout = nil: AssigningniltocollectionViewLayoutafter initialization also crashes. Always provide a valid layout object. - Forgetting to register cells: After fixing the layout crash, the next crash is usually
cell not registered. Always callregister(_:forCellWithReuseIdentifier:)beforereloadData.
Summary
UICollectionViewrequires a non-nilUICollectionViewLayoutat initialization — no exceptions- Use
UICollectionView(frame:collectionViewLayout:)with aUICollectionViewFlowLayoutorUICollectionViewCompositionalLayout - For
UICollectionViewController, pass the layout ininit(collectionViewLayout:) - In storyboards, verify the layout type and custom class are set correctly
- Change layouts after init with
setCollectionViewLayout(_:animated:)or direct assignment
Related reading
- UICollectionView reloadData not functioning properly in iOS 7
- UICollectionView Self Sizing Cells with Auto Layout
- 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
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.