Has anyone ever had trouble pushing to another controller that inherits the UICollectionViewController class? The problem I am having is I keep getting an error that states 'UICollectionView must be initialized with a non-nil layout parameter'.
Code for the button: This lives in my MenuController class
lazy var equipmentButton: UIButton = {
let eButton = UIButton()
eButton.backgroundColor = UIColor.white
eButton.setTitle("Equipment", for: .normal)
eButton.setTitleColor(UIColor.black, for: .normal)
eButton.frame = CGRect(x: 193.5, y: 76, width: 169.5, height: 106.2)
eButton.translatesAutoresizingMaskIntoConstraints = false
eButton.addTarget(self, action: #selector(handleEquipmentController), for: .touchUpInside)
return eButton
}()func handleEquipmentController() {
navigationController?.pushViewController(EquipmentController(), animated: true)
}In my EquipmentController I am inheritting UICollectionViewController and UICollectionViewDelegateFlowLayout using the following methods.
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 200)
}Below is the setup for the UICollectionViewCell
class EquipmentController: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
let thumbnailImageView: UIImageView = {
let imageView = UIImageView()
imageView.backgroundColor = UIColor.blue
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
let seperatorView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.gray
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let userProfileImageView: UIImageView = {
let imageView = UIImageView()
imageView.backgroundColor = UIColor.green
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
func setupViews() {
addSubview(thumbnailImageView)
addSubview(seperatorView)
addSubview(userProfileImageView)
let views = ["v0":thumbnailImageView]
let formatStringHorizontal = "H:|-16-[v0]-16-|"
let formatStringVertical = "V:|-16-[v0]-16-|"
let horizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: formatStringHorizontal, options:.alignAllCenterX, metrics: nil, views: views)
let verticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: formatStringVertical, options: .alignAllCenterX, metrics: nil, views: views)
let seperatorViews = ["v0":seperatorView]
let formatStringHorizontalSeperator = "H:|[v0]|"
let formatStringVerticalSeperator = "V:[v0(1)]|"
let horizontalSeperatorConstraint = NSLayoutConstraint.constraints(withVisualFormat: formatStringHorizontalSeperator, options: .alignAllCenterX, metrics: nil, views: seperatorViews)
let verticalSeperatorConstraint = NSLayoutConstraint.constraints(withVisualFormat: formatStringVerticalSeperator, options: .alignAllCenterX, metrics: nil, views: seperatorViews)
let userProfileViews = ["v0":userProfileImageView]
let formatStringHorizontalProfile = "H:|-16-[v0(44)]"
let formatStringVerticalProfile = "V:|-8-[v0(44)]"
let horizontalProfileConstraint = NSLayoutConstraint.constraints(withVisualFormat: formatStringHorizontalProfile, options: .alignAllCenterX, metrics: nil, views: userProfileViews)
let verticalProfileConstraint = NSLayoutConstraint.constraints(withVisualFormat: formatStringVerticalProfile, options: .alignAllCenterX, metrics: nil, views: userProfileViews)
NSLayoutConstraint.activate(horizontalConstraint)
NSLayoutConstraint.activate(verticalConstraint)
NSLayoutConstraint.activate(horizontalSeperatorConstraint)
NSLayoutConstraint.activate(verticalSeperatorConstraint)
NSLayoutConstraint.activate(horizontalProfileConstraint)
NSLayoutConstraint.activate(verticalProfileConstraint)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(codeer:) has not been implemented")
}
}A lot of code I know, thanks if you made it this far! To reiterate the issue, the above code gives me the error list at the top IF I am coming from a different view controller via a button press. HOWEVER, to test my code, I copied the above code and put it into a new project where the view controller is the first view loaded and it works perfectly fine.
So given this long post, is there a current issue with using .pushViewController() to a collection view in Swift 3? Or is it just me. Really hoping its just me.
I am doing this programmatically and not via nib/xib/storyboards. Just the way I prefer it. Constructive feedback and criticism is welcomed.
Thanks
-Tom G-