Mixing programmatic UI code with storyboards and xibs.
// register collection view cell
podcastView.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "podcastCell")
// register collection view cell using xib/nib
podcastView.collectionView.register(UINib(nibName: "PodcastCell", bundle: nil), forCellWithReuseIdentifier: "podcastCell")
NB: Reminder, a colleciton view must have a layout.
// collection view
public lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.itemSize = CGSize(width: 400, height: 400)
let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
cv.backgroundColor = .yellow
return cv
}()
guard let podcastDetailController = podcastDetailStoryboard.instantiateViewController(identifier: "PodcastDetailController") as? PodcastDetailController else {
fatalError("could not downcast to PodcastDetailController")
}
navigationController?.pushViewController(podcastDetailController, animated: true)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// override the default values of the itemSize layout from the collectionView property initializer in the PodcastView
let maxSize: CGSize = UIScreen.main.bounds.size
let itemWidth: CGFloat = maxSize.width * 0.95 // 95% of the width of device
return CGSize(width: itemWidth, height: 120)
}
navigationController?.navigationBar.prefersLargeTitles = true