Is it possible to increase the UICollectionViewLayout viewport? (i.e. load +2/-2 cells outside the UICollectionView frame)

StackOverflow https://stackoverflow.com/questions/23660928

Question

I have a full screen Image gallery working using UICollectionView. It is paged and it all runs ok but there is one thing I don't like.

The cells are reused only when they get in the visible viewport and I get a flickering effect when the new image is assigned (normal for the current behaviour).

How do I specify a viewport that is slightly larger than the current UICollectionView frame so that I load and reuse +1/-1 pages that are outside of view? When they get into view they'll already be initialized and ready to go so the flicker effect will go away.

Thanks!

Was it helpful?

Solution

NOTE: This is assumed vertical, adjust for horizontal

You can make the frame larger and then add a contentInset

CGFloat offset = 40.0; // or whatever works

CGRect yourFrame = yourCollectionView.bounds;
yourFrame.size.height += offset;
yourCollectionView.bounds = yourFrame;

CGFloat halfOffset = offset / 2.0;
yourFlowLayout.sectionInset = UIEdgeInsetsMake(halfOffset, 0, halfOffset, 0);

You will have to place these according to your code, but here's what we're doing. We're extending your frame a little bit so that cell reuse happens outside the visible bounds. By adding the insets to your flow layout, you allow the content to still align properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top