I have done a View in CollectionView with CustomLayout. In iOS6 it worked great but iOS7 it throws an exception like this.
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'layout attributes for supplementary item at index path ( {length = 2, path = 0 - 0}) changed from CustomSupplementaryAttributes: 0xd1123a0 index path: (NSIndexPath: 0xd112580 {length = 2, path = 0 - 0}); element kind: (identifier); frame = (0 0; 1135.66 45); zIndex = -1; to CustomSupplementaryAttributes: 0xd583c80 index path: (NSIndexPath: 0xd583c70 {length = 2, path = 0 - 0}); element kind: (identifier); frame = (0 0; 1135.66 45); zIndex = -1; without invalidating the layout'
You need to invalidate the existing layout before updating, see the end of the error message:
Apple Documentation for UICollectionViewLayout
I had this problem too, because I had code that depended on the content size of the collection view. My code was accessing the content size via the
collectionView!.contentSize
instead ofcollectionViewContentSize
.The former uses the
collectionView
property ofUICollectionViewLayout
, while the latter uses the custom-implemented layout property. In my code, the first time the layout was asked for attributes,contentSize
had not been set yet.I had the same exception: in iOS 7, you need now to override the inherited
isEqual:
in your UICollectionViewLayoutAttributes subclass as stated in Apple documentation here.iOS 10
At iOS 10, a new feature is introduced, it is
Cell Prefetching
. It will let dynamic position ofSupplementaryView
crash. In order to run in the old behavior, it needs to disableprefetchingEnabled
. It'strue
by default at iOS 10.I hate this problem. I spend 2 days for this problem. A reference about Cell Pre-fetch @iOS 10.
iOS 9 and before ...
@Away Lin is right.. I solve the same problem by implementing that delegate method.
My
Custom UICollectionViewLayout
will modify the attributes inlayoutAttributesForElementsInRect
. The section position is dynamic, not static. So, I obtain warnings about thelayout attributes for supplementary item at index path ... changed from ... to ...
. Before the changes,invalideLayout
related methods should be called.And, after implementing this delegate method to return
true
, the methodinvalidateLayoutWithContext:
will be called when scrolling theUICollectionViewLayout
. By default, it returnsfalse
.From Apple Docs
And more ...
A nice example project on GitHub, for custom UICollectionViewLayout.
I'm not entirely certain how or why, but this appears to be fixed in iOS 12, supporting both supplementary view resizing and prefetching. The trick for me was to make sure things are happening in the correct order.
Here is a working implementation of a stretchable header view. Notice the implementation of the header resizing happening in
layoutAttributesForElements(in rect: CGRect)
:P.S.: I'm aware the cache doesn't actually serve any purpose at this point :)
I solved my problem by override the method at the subclase of
UICollectionViewFlowLayout
:return YES