This is driving me crazy! I have a UICollectionViewController as shown below:
class PhrasesCompactCollectionViewController: UICollectionViewController
In Swift 3+ Use this method:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:(collectionView.frame.height-90)/2, height: 100)
}
Make sure that your class complies both Delegate
UICollectionViewDelegate
UICollectionViewDelegateFlowLayout
For me (in Swift 3.1) the method must be declared public like this:
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize()
}
Don't forget to make it public.
my problem was that I had my UICollectionViewDataSource object created as local variable. So if you initialize your dataSource object manually in code, make sure to store it as global variable.