UICollectionView滚动到任何页脚或标题视图

时间:2014-08-08 10:41:36

标签: ios objective-c cocoa-touch uicollectionview

我想滚动到集合视图的页脚或标题视图,但是scrollToItemAtIndexPath的标准方法仅滚动到单元格

- (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];
        }
}

如何滚动到任何页脚,标题视图,类似于滚动到单元格?

3 个答案:

答案 0 :(得分:8)

我知道这是一个老问题,但我最近遇到了同样的问题。我找到的最好的解决方案来自Gene De Lisa http://www.rockhoppertech.com/blog/scroll-to-uicollectionview-header/当你似乎在Obj-C工作时,这里是我使用的Swift代码的端口:

-(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];
}

上面的代码将正确滚动到给定部分的标题(我需要的所有内容)。修改此内容以滚动到页脚是非常简单的:

-(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];
}

答案 1 :(得分:4)

我最近遇到了同样的问题。这是我在 Swift 4 中的解决方案,以模仿scrollToItem(at:at:animated:)但使用页眉和页脚的行为。随意改进:

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)
    }
}

这里提供的要点: https://gist.github.com/aymericbaur/3256e25e33b64c3e5d380febf832db07

我希望它有所帮助。

答案 2 :(得分:0)

老问题,但看起来仍然是相关的。 对我来说 - @ mojoTosh的解决方案不起作用,但以下是:(Swift 4)

 if let attributes = self.collectionView?.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: indexPath),
                        let cellAttributes = self.collectionView?.layoutAttributesForItem(at: indexPath) {
                        let scrollingPosition = CGPoint(x: 0.0, y: cellAttributes.frame.origin.y - attributes.frame.size.height - collectionView.contentInset.top)
                        self.collectionView?.setContentOffset(scrollingPosition, animated: true)
相关问题