UICollectionViewLayout prepareLayout is called for any scroll

This weird behavior was detected when implementing a custom subclass of UICollectionViewLayout . I set subclass methods other than collectionViewContentSize . All cells appeared where I expected, but the contentView was too long. It looks like it will be twice as much as it should be.

I applied the method below to get the correct contentSize . Although, now the expected value, prepareLayout is called every time the view scrolls one pixel. This means that if I scroll from 0.0 to 0.500, prepareLayout is called 500 times!

What does this mean about my collectionViewContentSize , which can cause this?

 - (CGSize)collectionViewContentSize { UICollectionViewLayoutAttributes *leftAttributes = (UICollectionViewLayoutAttributes *)self.layoutInfo[@"segmentCell"][[NSIndexPath indexPathForItem:[self.collectionView numberOfItemsInSection:0]-1 inSection:0]]; UICollectionViewLayoutAttributes *rightAttributes = (UICollectionViewLayoutAttributes *)self.layoutInfo[@"segmentCell"][[NSIndexPath indexPathForItem:[self.collectionView numberOfItemsInSection:1]-1 inSection:1]]; float leftHeight = leftAttributes.frame.size.height + leftAttributes.frame.origin.y; float rightHeight = rightAttributes.frame.size.height + rightAttributes.frame.origin.y; float maxHeight = leftHeight > rightHeight ? leftHeight : rightHeight; return CGSizeMake(self.collectionView.bounds.size.width, maxHeight); } 
+6
source share
1 answer

Although according to the docs [0] shoulInvalidateLayoutForBoundsChange: supposed to return NO by default, it is not. As soon as I did this and returned NO in all cases, prepareLayout no longer called with a change in each border. This seems like an error in the UICollectionViewLayout.

[0] http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionViewLayout_class/Reference/Reference.html

+6
source

All Articles