I have a custom UICollectionViewLayout
that makes use of the self-sizing mechanism in iOS 8. My UICollectionViewCell
's implement preferredLayoutAttributesFittingAttributes
to return their preferred size, calculated using auto-layout. This works fine.
However, I was expecting preferredLayoutAttributesFittingAttributes
to also work for supplementary views, but it is never called. It is defined on UICollectionReusableView
after all.
If this mechanism is only for cells, what is the correct way to use auto-layout to size supplementary views in a custom UICollectionViewLayout
?
preferredLayoutAttributesFittingAttributes
will be called on supplementary views so long as they conform to the following requirements in your UICollectionViewLayout subclass (as far as I can tell!):
prepareLayout
layoutAttributesForSupplementaryViewOfKind:atIndexPath:
layoutAttributesForElementsInRect:
elementKind
matches up in your CollectionView's collectionView:viewForSupplementaryElementOfKind:atIndexPath:
when the supplementary view is dequeued via dequeueReusableSupplementaryViewOfKind:forIndexPath:
In summary: UICollectionViewLayout is a wicked beast but if tamed should be calling your supplementary view's preferredLayoutAttributesFittingAttributes
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With