iamarnold
iamarnold

Reputation: 229

Adding a UICollectionViewController as a child UIView crashes

I'm trying to add a custom UICollectionViewController in a child UIView and it crashes my app. Is there something wrong with my code or how my CustomCollectionViewController is implemented?

@IBOutlet weak var childView: UIView!
let myCollectionView = CustomCollectionViewController()



//inside viewDidLoad
self.addChildViewController(self.myCollectionView)
self.myCollectionView.didMoveToParentViewController(self)
self.myCollectionView.view.frame = self.childView.bounds<--crashes after executing this line
self.childView.addSubview(self.myCollectionView.view)
self.myCollectionView.delegate = self

error

Upvotes: 1

Views: 195

Answers (1)

Danny S
Danny S

Reputation: 21

This may be a little late, but initialize your CollectionViewController with a layout parameter.

Instead of:

let myCollectionView = CustomCollectionViewController()

use:

let myCollectionView = CustomCollectionViewController.init(collectionViewLayout: UICollectionViewFlowLayout())

That worked for me.

Upvotes: 2

Related Questions