Reputation: 4886
Trying to create a custom cell but when I am adding label in the cell collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath)
doesn't work.
Without label cell UI:
and the iPad simulator UI is:
But when I am adding label into the cell:
The iPad Simulator UI becomes :
In UICollectionViewDelegateFlowLayout
in the method:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let emptySpace = 87*2 + 10
let remainingWidth = Int(self.view.frame.width) - emptySpace
return CGSize(width: remainingWidth/2, height: remainingWidth/2)
}
Upvotes: 1
Views: 1897
Reputation: 294
Are you building with Xcode 11 (GM 2)? I had similar issues implementing sizeForItemAt
.
It seems something is causing the collection views to disregard the size returned in sizeForItemAt
and instead use the size of the child views.
What worked for me is setting Estimate Size
to None
on the collection view in Interface Builder.
This is not ideal, especially if you have many cells, but it was the only fix I found unfortunately.
Upvotes: 14