This is driving me crazy! I have a UICollectionViewController as shown below:
class PhrasesCompactCollectionViewController: UICollectionViewController
I had the same problem and nothing would fix it. I had a yellow warning saying to make it private because it came close to another function defined by the Layout Delegate. What fixed it for me was:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
Notice the difference from "sizeForItemAtIndexPath" to the correct "sizeForItemAt".
I tore my hair out for days trying to figure this out and it finally worked. Below I will include all of my function to show you how I also "hid" a cell by making the height equal to 0.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let findIndex = labelArray.index(of: "\(labelArray[indexPath.row])")
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
if (findIndex! % 2 == 0){
// Even index, show cell
return CGSize(width: screenWidth, height: 246)
}
else {
return CGSize(width: screenWidth, height: 0)
}
}