PreferredLayoutAttributesFittingAttributes does not require additional views

I have a custom UICollectionViewLayout that uses the self-size mechanism in iOS 8. My UICollectionViewCell implements preferredLayoutAttributesFittingAttributes to return their preferred size calculated using auto-layout. It works great.

However, I expected preferredLayoutAttributesFittingAttributes to work for extra views too, but it's never called. It is defined in the UICollectionReusableView in the end.

If this mechanism is intended only for cells, what is the correct way to use automatic layout to add additional views to a custom UICollectionViewLayout ?

+6
source share
1 answer

preferredLayoutAttributesFittingAttributes will be called in additional views if they meet the following requirements in your subclass of UICollectionViewLayout (as far as I can tell!):

  • An additional view is set with a non-zero frame in prepareLayout
  • Layout attributes are provided for an additional view in layoutAttributesForSupplementaryViewOfKind:atIndexPath:
  • An additional view is visible according to layoutAttributesForElementsInRect:
  • Finally, make sure that elementKind matches your CollectionView collectionView:viewForSupplementaryElementOfKind:atIndexPath: when the dequeueReusableSupplementaryViewOfKind:forIndexPath: is unloaded through dequeueReusableSupplementaryViewOfKind:forIndexPath:

In short: UICollectionViewLayout is an evil beast, but if a tame should call your preferredLayoutAttributesFittingAttributes additional view.

+1
source

Source: https://habr.com/ru/post/981217/


All Articles