Skip to content

Instantly share code, notes, and snippets.

@jparishy
Last active December 19, 2015 23:18
Show Gist options
  • Save jparishy/6033788 to your computer and use it in GitHub Desktop.
Save jparishy/6033788 to your computer and use it in GitHub Desktop.
Go fuck yourself, UICollectionView
@implementation FTTrack20TrackHomeFlowLayout
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
/*
* If the size of your UICollectionViewCell is larger than the screen height (
* I think? Consistently getting booted at height > 480) then it decides to remove
* the cell for reuse. We can prevent that by adjusting the frame returned for
* the layout attributes and the bounds of the actual cell view to make it seem
* like it's scrolling in place but without the bounds of the cell ever leaving
* the visible rect (which is what seems to trigger the removal).
*
* This is basically craziness. AFAICT it's a bug in UICollectionView that's been
* around for a while; this is simply a hacktastic workaround for now.
*/
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *layoutAttributesInRect = [super layoutAttributesForElementsInRect:rect];
CGPoint contentOffset = self.collectionView.contentOffset;
for(UICollectionViewLayoutAttributes *layoutAttributes in layoutAttributesInRect)
{
if(layoutAttributes.representedElementCategory == UICollectionElementCategoryCell)
{
if(CGRectGetHeight(layoutAttributes.frame) < CGRectGetHeight(UIScreen.mainScreen.bounds))
continue;
NSIndexPath *indexPath = layoutAttributes.indexPath;
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
CGRect frame = layoutAttributes.frame;
CGFloat originalOriginY = CGRectGetMinY(frame);
frame.origin.y = contentOffset.y;
layoutAttributes.frame = frame;
CGRect bounds = cell.bounds;
bounds.origin.y = contentOffset.y - originalOriginY;
cell.bounds = bounds;
}
}
return layoutAttributesInRect;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment