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); }
source share