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'
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())
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 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)
UICollectionView must be initialized with a non-nil layout parameter' bug
According to the docs , these are the valid initializers for a UICollectionViewController
init(collectionViewLayout: UICollectionViewLayout)
init(nibName: String?, bundle: Bundle?)
init?(coder: NSCoder)
In your scene delegate
, this is done fine:
let collectionVC
= CollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
However, in your CollectionViewController
, you don't use any of the viable initializers when initializing UISearchController
private let searchController
= UISearchController(searchResultsController: UICollectionViewController())
I believe it is this UICollectionViewController
you are trying to instantiate with an invalid initializer that is causing your problems
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
Dyld: Library Not Loaded: @Rpath/Mydsk.Framework/Mydsk -> Swift iOS 8.0
How to Create an Alert in a Swift File Model That Works for Various View Controller
How to Get Account Name from Contact Framework
How to Draw a Simple Rounded Rect in Swift (Rounded Corners)
Swift: Sort Array by Sort Descriptors
Swift - Uitableview Didselectrowatindexpath & Diddeselectrowatindexpath Add & Remove Indexpath Ids
Return Uiimage Array from Parse Query Block
Change Buttonstyle Modifier Based on Light or Dark Mode in Swiftui
How to Make Uiscrollview Zoom in Only One Direction When Using Auto Layout
How to Work with Udp Sockets in iOS, Swift
Xcode 9: Swift Dependency Analysis Error
How to Update Individual Array Element in Firebase with iOS Swift
How to Catch Nsunknownkeyexception in Swift 2.2
How to Call a View Controller Function from an iOS Appdelegate
Swift Computed Properties Cannot Be Used in Init
How to Properly Handle a Nil Uiapplication.Sharedapplication().Keywindow
How to Get Camera Calibration Data on iOS? Aka Avcameracalibrationdata