• Register
0 votes

Problem :

I have recently started learning about UICollectionView. I am very new to programming. I have also added the same UICollectionView by code to my App. I know that the CollectionCell is the custom class of the UICollectionViewCell.
But my app is now crashing with the following message: 

UICollectionView must be initialized with a non-nil layout parameter.”
I am tirelessly working to fix above error from many days but still unable to fix it. Is it possible for someone to help me in fixing above error?

8 5 2
3,230 points

Please log in or register to answer this question.

2 Answers

1 vote

Solution :

I can help you with this error. Your error message is clearly telling you about what is going wrong with your code:

If you get a chance to check the documentation related to UICollectionView, Then you will surely realize that the only initializer is you can find is initWithFrame:collectionViewLayout:. In your parameters for your initialize please stick to below rules:

A frame rectangle for your collection view which is measured in points. And the origin of your frame is relative to a superview in which you want to add it. Please note this frame is passed to your superclass during the initialization.

And a layout object is used for organizing items. Your collection view always stores the very strong reference to your specified object and it should not be nil.

So my suggestion is you should use the initWithFrame:collectionViewLayout: to initialize the UICollectionView, and you must make sure that you are passing it the non-nil UICollectionViewLayout object.

8 4 1
5,680 points
0 votes


The crash is saying you pretty apparently what is going wrong:

UICollectionView should be initialized by a non-nil layout parameter.

In case you control the documentation for UICollectionView, you'll trace that the solely initializer is initWithFrame:collectionViewLayout:. Moreover, in the parameters for that initializer, you'll see:


The frame rectangle for the recruitment sight, surveyed in points. The root of the frame is comparative to the super view in which you scheme to include it. This frame is received to the super class at the time of initialization.


The layout object to use with a view to  forming items. The collection sight stocks a strong reference to the marked object. It should not be nil.

I've bolded the significant portion. You should exercise initWithFrame:collectionViewLayout: to initialize your UICollectionView, and you should  exceed it a non-nil UICollectionViewLayout object.

One methos to correct this, then, would be to just change the order of initialization you do:

UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(100, 100);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];


 In the above instance, I've learned you want to exercise self.view.frame as the frame of self.collectionView. In case that's not the instance, put in  whatever frame you want instead.

Swift 3.0 and Swift 4.0


The app crashed as soon as it launched at the time UICollectionView is initialised

initialize  (above viewDidLoad)

let alarmCollectionView:UICollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout.init())
let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()

In Method (inside viewDidLoad)

layout.scrollDirection = UICollectionViewScrollDirection.vertical
alarmCollectionView.setCollectionViewLayout(layout, animated: true)
alarmCollectionView.delegate = self
alarmCollectionView.dataSource = self
alarmCollectionView.backgroundColor = UIColor.clear

It was worked fine after initialising like this.  I am employing Autolayout so using CGRect.zero or use your own CGRect


In case  you are employing UICollectionViewController instead of UICollectionView you require to include layout at the time initialization

 let  testNavController:UINavigationController = UINavigationController.init()
 let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout.init() 
 let collectionView:UICollectionViewController = UICollectionViewController.init(collectionViewLayout:layout )              
 testNavController.pushViewController(collectionView, animated: true)

Can employing UICollectionViewDelegateFlowLayout and don't forget to fix your cell size

class YourViewController : UICollectionViewController, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: view.frame.width, height: 250)
    override func viewDidLoad() {
        self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    // initialized with a non-nil layout parameter
    init() {
        super.init(collectionViewLayout: UICollectionViewFlowLayout())
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")

Perhaps at the time you offer your ChatViewController you have something like:

let chatVC = ChatViewController()

However, according to Apple doc:

class UICollectionViewController

At the time  you initialize the controller, employing the init(collectionViewLayout:) method, you mark the layout the collection view should have.

So you require:

let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

Apart from instantiating the vc with



shootDaysCollectionView(collectionViewLayout: UICollectionViewFlowLayout())

In case you want to configure

let lay = UICollectionViewFlowLayout()




10 6 4
31,120 points

Related questions

0 votes
1 answer 30 views
Problem: I am unable to understand this problem .. Please suggest me a better solution to solve it .. generic parameter 't' could not be inferred
asked Apr 6 Ifra 36.4k points
0 votes
1 answer 12 views
Problem: i ideas on what i need to do for this problem? non-aggregate type 'vector<int>' cannot be initialized with an initializer list
asked Apr 1 Rohit kr 17.5k points
0 votes
1 answer 4 views
Problem: I need your help with this. my function is: (the way it works is true) #include <iostream> using namespace std; #define V 4 #define INF 999 int floydWarshall(int graph[][V]){ int dist[V][V], i, j, k; for (i = 0; i < V; i++) for (j = 0; j ... dist[V][V]; } and this line has the error array must be initialized with a brace-enclosed initializer: int dist[V][V] = floydWarshall(graph[][V]);
asked 4 days ago anika11 32.2k points
0 votes
1 answer 2 views
Problem: I need your help with this. My function is: (the way it works is true) #include <iostream> using namespace std; #define V 4 #define INF 999 int floydWarshall(int graph[][V]){ int dist[V][V], i, j, k; for (i = 0; i < V; i++) for (j = 0; j ... dist[V][V]; } and this line has the error array must be initialized with a brace-enclosed initializer: int dist[V][V] = floydWarshall(graph[][V]);
asked 5 days ago muktaa 34.6k points
0 votes
1 answer 278 views
Problem: I am naive to C++. I have written code in C++ with the help of online tutorials. But I am facing the below C++ error if I try to execute my code: Array must be initialized with a brace enclosed initializer. I am receiving the above error from the ... + code: int cipher[Array_size][Array_size] = 0; Do you see any problem in my above line of code? Please explain to me the above error.
asked Sep 8, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 7 views
Problem: I have a simple object which conforms to the NSCoding protocol. Any idea what's causing this? "[User defaults] attempt to set a non-property-list object."
asked Apr 12 anika11 32.2k points
0 votes
1 answer 3 views
Problem: I am trying to compile my code in Xcode 9 which is written in swift. I am using pod -> PhotoEditorSDK and it continiuosly shows me this error. Module compiled with Swift 3.1 cannot be imported in Swift 3.2: /Users ... /Examples/SwiftExample/Pods/PhotoEditorSDK/pesdk-ios-build-7.3.0/PhotoEditorSDK.framework/Modules/PhotoEditorSDK.swiftmodule/arm64.swiftmoduleIs there any fix for this?
asked 4 days ago Supti 13k points
0 votes
1 answer 3 views
Problem: After updating to Xcode 9, I tried to build one of my projects. I use the FacebookLogin pod. I have a compiler error in FacebookLogin/LoginButton.swift .Please help me to solve this problem.
asked 4 days ago salim1 60.6k points
0 votes
1 answer 4 views
Problem: Please provide me solution for thos problem,"3.2 Apps with placeholder text will be rejected " .
asked Apr 25 zayed1 61.8k points