Is there a way to return the size calculated by constraints in the sizeForItemAt UICollectionViewDelegateFlowLayout method

144 views Asked by At

I'm overriding the sizeForItemAt method of UICollectionViewDelegateFlowLayout. What I'd like to do is only calculate the size if I'm in a certain section, and fall back to the default flow layout behaviour for the other sections.

I'm not entirely sure how to go about it, as I'm not subclassing I can't just use the super class implementation.

A point in the right direction would be much appreciated.

EDIT

I wanted to use systemLayoutSizeFitting as demonstrated below, but the cell isn't created when sizeForItemAt is called.

guard indexPath.section == sectionNumber else {
  // This always returns nil as the cell hasn't been created when sizeForItemAt runs
  guard let cell = collectionView.cellForItem(at: indexPath) else {
    return .zero
  }

  return cell.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
}

return someCalculatedCGSize

I've tried creating my own layout subclass but I run into the same issues, I have two full width cells which auto layout provides constraints for and then a grid layout below which I manually calculate sizes for. So I'd like to fall back to my constraints providing the size for the first two cells.

0

There are 0 answers