Reputation: 231
I'm trying to use UICollectionView to display the view to the left and right of the current view that is centered in the screen.
I have this displaying properly but the horizontal paging does not center on the subsequent views because it defaults to the width of the frame, which is 320.0.
Where is UICollectionView calculating the default offset value it uses when clipping to the next pages?
I would like to change this value. Is there a better way to do this? Essentially I am trying to recreate the experience being used in the search results for the app store on the iphone.
Upvotes: 7
Views: 14124
Reputation: 1
try to set contentOffset in viewDidLayoutSubviews
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (viewDidLayoutSubviewsForTheFirstTime) {
_collectionView.contentOffset = CGPointMake(...);
}
}
Upvotes: 0
Reputation: 5132
I think that the best solution is to add a custom UICollectionViewFlowLayout subclass and override the
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)contentOffset
withScrollingVelocity:(CGPoint)velocity
I wrote an example here: https://gist.github.com/mmick66/9812223
Upvotes: 5
Reputation: 231
It turned out that I had to set pagingEnabled = NO
and overrode (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
with the following:
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
if (self.lastQuestionOffset > scrollView.contentOffset.x)
self.currentPage = MAX(self.currentPage - 1, 0);
else if (self.lastQuestionOffset < scrollView.contentOffset.x)
self.currentPage = MIN(self.currentPage + 1, 2);
float questionOffset = 290.0 * self.currentPage;
self.lastQuestionOffset = questionOffset;
[self.collectionView setContentOffset:CGPointMake(questionOffset, 0) animated:YES];
}
This answer helped: Paging UIScrollView with different page widths
Upvotes: 11