UICollectionView scrollToItemAtIndexPath UICollectionView scrollToItemAtIndexPath ios ios

UICollectionView scrollToItemAtIndexPath


Make sure the collection view has laid out its subviews first along with the resizing of the cell before scrolling. I would suggest moving your scrolling to a place where you're sure all layouts have been completed such as:

- (void)viewDidLayoutSubviews{    [super viewDidLayoutSubviews];    NSIndexPath *indexPath = // compute some index path    [self.collectionView scrollToItemAtIndexPath:indexPath                                atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally                                        animated:YES];}


Another option is to call layoutIfNeeded on the UICollectionView before you call scrollToItemAtIndexPath. That way you won't perform the scroll operation every time the parent view's subviews are laid out.