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

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

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!

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top