UICollectionView using estimatedItemSize

I have a collection view with quite simple layout: vertical flow layout. There is a set of full width cells. All of them are using NSAutoLayout constraints. On the reload collectionviewlayout is calling itemSize for each of these cells. It causes performance hit if we have a lot of cells there or layout in each cell quite complicated.

I decided to use new feature introduced in iOS8: property estimateditemSize of UICollectionViewFlowLayout. I also overrided preferredLayoutAttributesFittingAttributes method:

- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes  {
     UICollectionViewLayoutAttributes *preferredAttributes = [super preferredLayoutAttributesFittingAttributes: layoutAttributes];
     UICollectionViewLayoutAttributes *attributes = [preferredAttributes copy];
     CGRect newFrame = attributes.frame;
     [self setNeedsLayout];
     [self layoutIfNeeded];
     CGFloat desiredHeight = [self.contentView systemLayoutSizeFittingSize: UILayoutFittingCompressedSize].height;
     newFrame.size.height = desiredHeight;
     attributes.frame = newFrame;
     return attributes;
}


The problem is that form some cells layout is still using estimatedItemSize, that's why these cells are truncated. On iOS8 also some cells are just missed, I think they have wrong positions.

Overriding method of UICollectionViewFlowLayout

- (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
   return YES;
}

can resolve all issues for iOS9, but on iOS8 I have a disastrous performance. I'm not even able to scroll at all. And in general it's a bad approach because it will cause context invalidation during scrolling too often.

Any help and advices will be kindly appreciated.

UICollectionView using estimatedItemSize
 
 
Q