Reputation: 1349
I am trying to scroll in horizontal of a collection view section item. There may be multiple section in collection view.
Upvotes: 0
Views: 932
Reputation: 5247
Did you try this method?
-(void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated
Or, besides scrolling to item, you can scrollRectToVisible:sectionRect. This example scrolls to a first item in a given section also considering the header view.
- (void)scrollToSection:(NSUInteger)section
{
if ([self.collectionView numberOfItemsInSection:section] > 0) {
UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
CGRect sectionRect = CGRectMake(0, attributes.frame.origin.y - 65, self.collectionView.frame.size.width, self.collectionView.frame.size.height); // 65 is height of the header view
[self.collectionView scrollRectToVisible:sectionRect animated:YES];
} else {
NSLog(@"no cell to scroll to");
}
}
Upvotes: 1