【问题标题】:UICollectionView scroll to any footer or header viewUICollectionView 滚动到任何页脚或页眉视图
【发布时间】:2014-10-01 19:46:42
【问题描述】:

我想滚动到集合视图的页脚或页眉视图,但是,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];
        }
}

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

【问题讨论】:

    标签: ios objective-c cocoa-touch uicollectionview


    【解决方案1】:

    我知道这是一个老问题,但我最近遇到了同样的问题。我找到的最佳解决方案来自 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];
    }
    

    【讨论】:

      【解决方案2】:

      我最近遇到了同样的问题。这是我在 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

      希望对你有帮助。

      【讨论】:

        【解决方案3】:

        老问题,但看起来仍然相关。 对我来说 - @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)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-01
          • 2014-11-08
          • 1970-01-01
          相关资源
          最近更新 更多