6

Per my understanding about self sizing cells in a uicollectionview with a custom layout, informed by great articles such as this one by Richard Turton, the way to deal with self sizing cells is via

  1. Set an estimated size for your cells/views in prepare, since they will have a chance to update themselves with the real autolayout calculations
  2. provide preferredLayoutAttributesFitting in the cells themselves where the cell calculates the preferred attributes (frame),
  3. the layout repeatedly invalidates itself until those are all covered via invalidationContext(forPreferredLayoutAttributes:withOriginalAttributes)

On initial load, this seems to work fine for me. However, on changes driven by a call to collectionView.performBatchUpdates, I either see a flash of the cells at the estimated size set in prepare, or it sometimes ends on that (if I'm trying to animate them in with an initial size).

I followed the flow, and I see the attributes are getting correctly provided via the invalidation flow. prepare is called multiple times though (including after the attributes were properly set via preferred attribute invalidation). This seems to stomp on the correct size, at least temporarily, causing an animation glitch (or a bad final layout).

The only working solutions I've been able to find on the interwebs where a performBatchUpdates is called, skip trying to use the preferred Attributes methods and just call out to a delegate method on the UICV inside the prepare method to get the proper size right at that step, but that seems to go against the tooling is provided.

Perhaps a known issue

FYI, spoke with a couple collection view engineers and this appears to be a bug. They recommend the workaround of doing manual sizing with a reference cell.

Bob Spryn
  • 17,742
  • 12
  • 68
  • 91
  • "They recommend the workaround of doing manual sizing with a reference cell." What does this mean? – Tricky Feb 08 '19 at 01:27

1 Answers1

0

I had a similar issue with the floating headers in collectionview. It might help check: UICollectionView sticky header disappears for while after inserting section when collection is overscrolled (bounce effect)