UICollectionView: must be initialized with a non-nil layout parameter
The crash is telling you pretty explicitly what is going wrong:
UICollectionView must be initialized with a non-nil layout parameter.
If you check the documentation for UICollectionView
, you'll find that the only initializer is initWithFrame:collectionViewLayout:
. Further, in the parameters for that initializer, you'll see:
frame
The frame rectangle for the collection view, measured in points. The origin of the frame is relative to the superview in which you plan to add it. This frame is passed to the superclass during initialization.
layout
The layout object to use for organizing items. The collection view stores a strong reference to the specified object. Must not be nil.
I've bolded the important part. You must use initWithFrame:collectionViewLayout:
to initialize your UICollectionView
, and you must pass it a non-nil UICollectionViewLayout
object.
One way to fix this, then, would be to simply 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"];
Note that in the above example, I've assumed you want to use self.view.frame
as the frame of self.collectionView
. If that's not the case, insert whatever frame you want instead.
UICollectionView must be initialized with a non-nil layout parameter'
Instead of instantiating the vc with
shootDaysCollectionView()
use
shootDaysCollectionView(collectionViewLayout: UICollectionViewFlowLayout())
if you want to configure
let lay = UICollectionViewFlowLayout()
lay...
shootDaysCollectionView(collectionViewLayout:lay)
Error: UICollectionView must be initialized with a non-nil layout parameter
Probably when you present your ChatViewController you have something like:
let chatVC = ChatViewController()
But according to Apple doc:
class UICollectionViewController
When you initialize the controller, using the init(collectionViewLayout:) method, you specify the layout the collection view should have.
So you need:
let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())
Thread 1: UICollectionView must be initialized with a non-nil layout parameter
Why are you adding in your View Controller:
var newCollection = UICollectionView()
Since you already added it in your view?, instead you should set your view as your view type, and reference that collection view from the your View Controller.
var myView: CustomView
myView = MyView()
view = myView
let collectionView = myView.collectionView
Every collection view needs a UICollectionViewLayout subclass in its initializer, now UICollectionViewLayout is a abstract class, you can make your own, but you could also use UICollectionViewFlowLayout()
in this case:
var newCollection = UICollectionView(collectionViewLayout: UICollectionViewFlowLayout())
Now you did add it on your first view, but not in the second one, again check why the second one exists, that's the one causing the problems.
UiCollectionView must be initialized with a non-nil layout parameter Swift 3
Your MyCollectionViewController class is a subclass of UICollectionViewController which has a property of collectionView which is the actual UICollectionView itself. So the separate UICollectionView you create in this line:
let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
is not the same as self.collectionView which is the one provided by the UICollectionViewController itself.
So this is not the source of the error you are getting.
In fact the error you are seeing is relayed to how you are creating and presenting the MyCollectionViewController itself. You don't show the code for how that is created but you are not passing any kind of layout to it. For example to create it in code you would do this:
let layout = UICollectionViewFlowLayout()
let myCollectionVC = MyCollectionViewController(collectionViewLayout: layout)
UICollectionViewController Error in Swift 3.0: must be initialized with a non-nil layout parameter
Thank you! I incorporated your suggestions. Below is the code that works:
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let cellId = "CellId"
var colView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 111, height: 111)
colView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
colView.delegate = self
colView.dataSource = self
colView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
colView.backgroundColor = UIColor.white
self.view.addSubview(colView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 21
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.orange
return cell
}
}
Again, thank you guys very much. Have an awesome day :D
UICollectionView must be initialized with a non-nil layout parameter'?
The error is clear. You need to specify a layout when creating a collection view but you do not do so.
In your convenience init
you call self.init()
but you must call self.init(frame:,collectionViewLayout:)
.
You will need to refactor your setupCollectionView
method as part of getting this correct.
UICollectionView must be initialized with a non-nil layout parameter' problem arises even when layout parameter is set
How about something like this?
private lazy var collectionView: UICollectionView = {
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 188.0, height: 268.0)
let result: UICollectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
result.register(CellClass.self, forCellWithReuseIdentifier: "cell")
result.dataSource = self
result.delegate = self
return result
}()
Related Topics
Can't Cast Value of Type Uiviewcontroller to Patterndetailviewcontroller
How to Get the Incoming Call Number by Using Callkit
Tableview Cell How Do We Resize Cell in Swift Along with Image and Label
How to Make an iOS Asset Bundle
Arkit Hide Objects Behind Walls
Facebook Graph API Get Request - Should Contain "Fields" Parameter (Swift, Facebook Sdk V4.5.1)
How Shouldchangecharactersinrange Works in Swift
Setting Constraints Programmatically
iOS Uiimage Storage Formats, Memory Usage and Encoding/Decoding
Memory Leak Every Time Uiscrollview Is Released
Hardware-Dependent Nsdateformatter Datefromstring: Bug (Returns Nil)
Creating Thumbnail from Local Video in Swift
How to Add a Button to Uinavigationbar
How to Change the Color of an Uiimage
How to Put the Uipagecontrol Element on Top of the Sliding Pages Within a Uipageviewcontroller
How to Programmatically Use iOS Voice Synthesizers? (Text to Speech)