Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView Paging Offsets and Spacing

I'm stumped. I've got this collection view showing the edges of cells as a hint for what's available with a swipe, but only the first and last cells appear properly. Everything in between isn't lining up properly. What am I missing?

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(280, 400);
layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
layout.minimumInteritemSpacing = 10;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.minimumLineSpacing = 10.0;

self.collectionView.collectionViewLayout = layout;
self.collectionView.showsHorizontalScrollIndicator = NO;
self.collectionView.pagingEnabled = YES;
self.collectionView.clipsToBounds = NO;

enter image description here

like image 222
E-Madd Avatar asked Mar 10 '14 20:03

E-Madd


1 Answers

I just ran into the same issue. The problem, as I understand it, is that paging distance with UIScrollView is determined by the width of the scroll view. UICollectionView is a UIScrollView.

I've found solutions to accomplish this peeking feature with a UIScrollView by making the UIScrollView narrower than your screen, turning on pagingEnabled, and turning off clipsToBounds. To capture gestures from the edges of the screen, you can then embed the UIScrollView into a full-screen-width UIView that captures and passes gestures to the UIScrollView. See here.

Since UICollectionView is a UIScrollView, I attempted the same approach with the UICollectionView. Unfortunately with UICollectionView the adjacent cells only appear when they start to enter the UICollectionView (which was narrower than the screen in my case). Yes, even with clipsToBounds off. It just doesn't bother rendering any part of the cell until at least some part of the cell is visible within the UICollectionView.

So I've made my UICollectionView full-screen-width. I'm now on a hunt to find a good way to customize the paging.

If that doesn't turn up with a slick solution, I will probably drop back to replacing the UICollectionView with a UIScrollView and layout my "cells" side-by-side by hand.

I'm also a bit concerned that we might run into problems with UICollectionView in this scenario since we don't have full control over the cell spacing. Notice you can only set the minimum and not a precise value.

like image 54
davew Avatar answered Nov 13 '22 18:11

davew