THIS IS iOS 15 ONLY - THE SAME CODE WORKS FOR iOS 14.x USING XCODE 13.x, see below for details
When using background elements with UICollectionViewFlowLayout and UICollectionViewCompositionalLayout in iOS 15 the following happens on .reloadData(), .reconfigureItems(...), and .reloadItems(...) only (initial load is fine):
collectionView:viewForSupplementaryElementOfKind:atIndexPath: is called for background view
Background view is dequeued SUCCESSFULLY
viewForSupplementaryElementOfKind returns the view
App crashes post viewForSupplementaryElementOfKind (sometimes up to seconds later) saying viewForSupplementaryElementOfKind has returned null
I have verified:
viewForSupplementaryElementOfKind IS dequeueing a cell
The cell IS the correct element
The element is returning
viewForSupplementaryElementOfKind IS valid
viewForSupplementaryElementOfKind loads correctly when the collectionView first loads; this only occurs on refreshing
PLEASE, re-read the very top statement again before replying to ensure you are not replying "you/viewForSupplementaryElementOfKind is returning nil, check you are not returning nil". I will happily run additional test.
Post
Replies
Boosts
Views
Activity
Our code worked for iOS 14.x with Xcode 12.5.x. Updating to Xcode 13.x, we are receiving the following crash anytime we attempt to call collectionView.reloadData() or collectionView.setContentOffset(...).
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath: does not match the element kind it is being used for. When asked for a view of element kind 'HomeSectionBackgroundView' the data source dequeued a view registered for the element kind '(null)'.'
If we replace .reloadData() with .reloadItems(_: ) or .reconfigureItems(_: ), it fixes one of two errors, but how would we correct the .setContentOffset(...) crash?