I have a simple UICollectionView with a UICollectionViewFlowLayout that uses autolayout to allow for self-sizing cells. Things work great in a static layout, but when I insert or remove cells, the cells resize to the estimated size:
[self.collectionView performBatchUpdates:^{
[self.collectionView insertItemsAtIndexPaths:@[indexPath]];
} completion:nil];
Beyond setting the estimated size, like this:
layout.estimatedItemSize = CGSizeMake(88.0, 88.0);
I'm not doing anything fancy in the collection view. Everything is set up using storyboards.
How can I avoid this resizing? I'm only targeting iOS 8.0 and up so it should be supported.
I ran into this, and it took me a while to figure out that the size it was reverting to was the estimated size.
I think I figured out a workaround:
sizeForItemAtIndexPath
, and configure that cell instance with data for that index path, then ask it for its preferred size (systemLayoutFittingSize
).See example project here.
Set estimatedItemSize
property to some appropriate size.
In your custom UICollectionView
class override below method and cache layoutAttributes's size given by super.
-(UICollectionViewLayoutAttributes*)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
UICollectionViewLayoutAttributes * superAttr = [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
//Cache size which is returned by super (superAttr.frame.size) since super calculates the correct size for auto size cells.
return superAttr;
}
3.In sizeForItemAtIndexPath return cached size if available else estimated size.
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//return cached size if available else the estimated size
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With