Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView scroll to any footer or header view

I want to scroll to footer or header view of the collection view, however, the standard approach with scrollToItemAtIndexPath scrolls only to the cells

- (void)scrollToBottom {
        NSInteger section = [self numberOfSectionsInCollectionView:self.collectionView] - 1;
        NSInteger item = [self collectionView:self.collectionView numberOfItemsInSection:section] - 1;
        if ((section > 0) && (item > 0)) {
            NSIndexPath * lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
            [self.collectionView scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];
        }
}

How to scroll to any footer, header view, similar as with scrolling to cell?

like image 525
Peter Lapisu Avatar asked Aug 08 '14 10:08

Peter Lapisu


2 Answers

I know this is an old question now, but I recently had the same issue. The best solution I found was from Gene De Lisa at http://www.rockhoppertech.com/blog/scroll-to-uicollectionview-header/ As you appear to be working in Obj-C, here's the port of his Swift code that I use:

-(void) scrollToSectionHeader:(int)section {    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:section];
    UICollectionViewLayoutAttributes *attribs = [self.collectionView layoutAttributesForSupplementaryElementOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
    CGPoint topOfHeader = CGPointMake(0, attribs.frame.origin.y - self.collectionView.contentInset.top);
    [self.collectionView setContentOffset:topOfHeader animated:YES];
}

The code above will properly scroll to a given section's header (all I needed). It's trivial to modify this to scroll to a footer instead:

-(void) scrollToSectionFooter:(int)section {    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:section];
    UICollectionViewLayoutAttributes *attribs = [self.collectionView layoutAttributesForSupplementaryElementOfKind:UICollectionElementKindSectionFooter atIndexPath:indexPath];
    CGPoint topOfFooter = CGPointMake(0, attribs.frame.origin.y - self.collectionView.contentInset.top);
    [self.collectionView setContentOffset:topOfFooter animated:YES];
}
like image 68
MojoTosh Avatar answered Oct 16 '22 23:10

MojoTosh


I recently had the same issue. Here is my solution in Swift 4 to mimic the behavior of scrollToItem(at:at:animated:) but with Headers and Footers. Feel free to improve:

func scrollToSupplementaryView(ofKind kind: String, at indexPath: IndexPath, at scrollPosition: UICollectionViewScrollPosition, animated: Bool) {
    self.layoutIfNeeded();
    if let layoutAttributes =  self.layoutAttributesForSupplementaryElement(ofKind: kind, at: indexPath) {
        let viewOrigin = CGPoint(x: layoutAttributes.frame.origin.x, y: layoutAttributes.frame.origin.y);
        var resultOffset : CGPoint = self.contentOffset;

        switch(scrollPosition) {
        case UICollectionViewScrollPosition.top:
            resultOffset.y = viewOrigin.y - self.contentInset.top

        case UICollectionViewScrollPosition.left:
            resultOffset.x = viewOrigin.x - self.contentInset.left

        case UICollectionViewScrollPosition.right:
            resultOffset.x = (viewOrigin.x - self.contentInset.left) - (self.frame.size.width - layoutAttributes.frame.size.width)

        case UICollectionViewScrollPosition.bottom:
            resultOffset.y = (viewOrigin.y - self.contentInset.top) - (self.frame.size.height - layoutAttributes.frame.size.height)

        case UICollectionViewScrollPosition.centeredVertically:
            resultOffset.y = (viewOrigin.y - self.contentInset.top) - (self.frame.size.height / 2 - layoutAttributes.frame.size.height / 2)

        case UICollectionViewScrollPosition.centeredHorizontally:
            resultOffset.x = (viewOrigin.x - self.contentInset.left) - (self.frame.size.width / 2 - layoutAttributes.frame.size.width / 2)
        default:
            break;
        }
        self.scrollRectToVisible(CGRect(origin: resultOffset, size: self.frame.size), animated: animated)
    }
}

Gist available here: https://gist.github.com/aymericbaur/3256e25e33b64c3e5d380febf832db07

I hope it helps.

like image 38
Aymeric Baur Avatar answered Oct 16 '22 23:10

Aymeric Baur