Home UICollectionView scrollToItemAtIndexPath
Reply: 0

UICollectionView scrollToItemAtIndexPath

user10291
1#
user10291 Published in September 19, 2018, 7:20 pm

I'm trying to scroll to a specific item in a collection view and it seems to happening properly about 90 % of the time. I basically have a collection view whose frame I change via auto layout and then I want my cell be the size of the all of the new frame, so I set a new size. When I put a breakpoint on the scrollToItemAtIndexPath, it seems when it works works the item size have taken effect, but the times it doesn't work, the cell still have the old size. How can I make sure the itemSize has changed before scrolling?

[weakSelf.view removeConstraints:@[weakSelf.verticalSpace, weakSelf.collectionViewBottomSpace, weakSelf.bottomLayoutTopCollectionView]];
[weakSelf.view addConstraints:@[weakSelf.collectionViewToTop, weakSelf.imageHeight, weakSelf.youLabelToTop]];
[UIView animateWithDuration:animated ? .5 : 0.0
                                      animations:^{
                                          [weakSelf.view layoutIfNeeded];
                                      }
                                      completion:^(BOOL finished) {
  UICollectionViewFlowLayout * layout = (UICollectionViewFlowLayout *)self.currentUserCollectionView.collectionViewLayout;

  layout.itemSize = weakSelf.currentUserCollectionView.frame.size;

  [weakSelf.currentUserCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:[self getSelectedIndex:selectItem] inSection:0]
                                         atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
                                                 animated:NO];
}];
share|improve this question
  • 2
    What is the question? You have just pasted code here. – Fogmeister Nov 6 '13 at 15:35
  • Sorry...pressed enter too soon – Venkat S. Rao Nov 6 '13 at 15:41
  • No worries, not my down vote btw :) – Fogmeister Nov 6 '13 at 15:45
  • When you change the layout constraints on the collection view are you calling invalidateLayout on it? – Fogmeister Nov 6 '13 at 15:46
  • I call layoutIfNeeded. I updated the code. – Venkat S. Rao Nov 6 '13 at 15:52

2 Answers 2

active oldest votes
up vote 40 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316962 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO