Desmond DAI
Desmond DAI

Reputation: 495

Swift3 - UICollectionViewCel's intrinsicContentSize is (-1, -1)

When I try to get cell's intrinsicContentSize in function collectionView(_:didSelectItemAt:), the result is (-1, -1).

I am using flow layout with auto layout config of cell's subviews and did not implement collectionView(_:layout:sizeForItemAt:). Does anyone have ideas?

Edit:

the auto layout of cell has fixed width and height. I turned on self-sizing by setting estimatedItemSize and config the collectionView as following:

self.collectionView.delegate = self
self.collectionView.dataSource = self
if let layout = self.collectionView.collectionViewLayout as? 
    UICollectionViewFlowLayout {
        layout.estimatedItemSize = CGSize(width: 100, height: 100)
}

And I try to get the cell's intrinsicContentSize in:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath)
    let size = cell?.intrinsicContentSize
}

which gives me (-1, -1) for size

Upvotes: 0

Views: 580

Answers (1)

matt
matt

Reputation: 535305

A UICollectionViewCell has no intrinsicContentSize. Why do you expect it to have one?

  • If you want to know what size the cell is at this moment, just ask for its bounds.size.

  • If you want to know what size the cell would take on if it were sized from inside by the autolayout constraints of its subviews, call systemLayoutSizeFitting(_:).

Upvotes: 1

Related Questions