Resize UICollectionView while scrolling

My browsing of the collection starts life short, at the bottom of the screen the view of the controller opens. When the user scrolls a bit, I would like the collection to fill the view. I would like it to shrink again when the user descends into the bounce area.

Here is the code I would like to do (and I think it should work):

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView == self.collectionView) { CGFloat offsetY = self.collectionView.contentOffset.y; if (offsetY > 20.0) { [self setCollectionViewExpanded:YES]; } else if (offsetY < -10.0) { [self setCollectionViewExpanded:NO]; } } } - (void)setCollectionViewExpanded:(BOOL)expanded { // avoid starting a do-nothing animation BOOL currentlyExpanded = self.collectionView.frame.origin.y == 0.0; if (expanded == currentlyExpanded) return; // note: the collection view is the uppermost subview of the vc view // expanding to view bounds makes it cover every other view CGRect newFrame = (expanded)? self.view.bounds : CGRectMake(0.0, 240.0, 320.0, self.view.bounds.size.height-240.0); [UIView animateWithDuration:0.3 animations:^{ self.collectionView.frame = newFrame; }]; } 

But after the multiplication occurs, the cells remain in the same position relative to the parent view (still near the bottom), and the collection view stops responding to panning calls and no longer sends scrolling notifications.

I can load the contents correctly by doing reloadData in the animation completion block, but the scroll bars stop working anyway.

A back, I tried something like this with a table view (or some other kind of scroll) and ran into a similar hang. It is very important if anyone decides ...

+4
source share
1 answer

Can you describe your view hierarchy a little better? Is a UICollectionView a preview of a UIScrollView?

Is self.view UIScrollView?

  • In this case, the origin is scrollview contentOffset (not 0,0). This can lead to an unexpected frame in your UICollectionView.
  • If the UICollectionView is a scroll sub, the start of the frame is the source of the view in the scroll space. Therefore, do not change the start of the frame (only its size) and set the contentOffset for the scrollView to make it visible.
0
source

All Articles