This is driving me crazy! I have a UICollectionViewController as shown below:
class PhrasesCompactCollectionViewController: UICollectionViewController
First class need confirm to UICollectionViewDelegateFlowLayout. Then you need to write following code in viewDidLoad():
//To tell the UICollectionView to use your UIViewController's UICollectionViewDelegateFlowLayout methods
collectionView.delegate = self
// If you want to set your own collection view flow layout
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical //depending upon direction of collection view
self.collectionView?.setCollectionViewLayout(layout, animated: true)
By using this code UICollectionViewDelegateFlowLayout method
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
will get called, you can set size in this method.