• 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

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
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 29 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 Raphael Pacheco 4.9k points
0 votes
1 answer 527 views
Problem : Facing following Swift issue must register a nib or a class for the identifier or connect a prototype cell in a storyboard
asked Nov 15, 2019 peterlaw 6.9k points
0 votes
1 answer 51 views
Problem: I am trying to archive my project but facing very unexpected error as shown below: Showing Recent Messages:-1: mkdir -p /Users/spritzindia/Library/Developer/Xcode/DerivedData/Contigo-atftiouzrdopcmcpprphpilawwzm/Build/Products/Debug-iphonesimulator/Contigo.app/Frameworks Showing ... : pod 'IQKeyboardManagerSwift'  pod 'SDWebImage', '~> 4.0'  pod 'KRPullLoader'  pod 'Paytm-Payments'
asked Aug 25 Raphael Pacheco 4.9k points
0 votes
1 answer 81 views
Problem : I have very simple program HelloWorld is running on iOS. The same program has been running fine for a long time. But recently I noticed that I get the below listed BoringSSL error when my program runs on my ... boringssl_session_errorlog: line 2871 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert
asked Dec 4, 2019 alecxe 7.5k points