First I have to give my two cents that the storyboards are great :)
You might not want to go with CollectionViewController for this use case. If you decide to use it, I also posted another answer. Here's the fastest way to move a CollectionView into a ViewController. This solves your problem, but does not take into account autorun.
1) Replace these lines in the ViewController:
let fsPicVC = CollectionViewController(collectionViewLayout: layout) self.present(fsPicVC, animated: true) { }
from
let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout) collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "Cell") collectionView.dataSource = self view.addSubview(collectionView)
2) Add this to the bottom of the ViewController (outside the ViewController class):
extension ViewController: UICollectionViewDataSource { func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 10 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
Finally, you can remove the CollectionViewController, as it is now replaced.
PS You also probably want to: 1) extend the ViewController to match the UICollectionViewDelegateFlowLayout and 2) make the collectionView global.
mmd1080
source share