Amit
Amit

Reputation: 4886

Custom collection view cell not working properly

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:

enter image description here

and the iPad simulator UI is:

enter image description here

But when I am adding label into the cell:

enter image description here

The iPad Simulator UI becomes :

enter image description here

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

Answers (1)

Alex Scanlan
Alex Scanlan

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.

setting <code>Estimated Size</code> to <code>None</code> on the collection view in Interface Builder

Upvotes: 14

Related Questions