【问题标题】:UICollectionView - Scrolling to first cellUICollectionView - 滚动到第一个单元格
【发布时间】:2015-08-28 02:45:28
【问题描述】:

我正在使用 UICollectionView,我需要在单击按钮后滚动到第一个单元格。

该按钮位于我的集合视图中的(大)标题部分...单击它时,我需要滚动到位于该按钮下方的第一个单元格。

我创建了这个按钮的动作,但我不知道如何滚动到第一个 UICollectionViewCell。

有人可以帮助我吗?

【问题讨论】:

    标签: ios swift xcode6 uicollectionview uicollectionviewcell


    【解决方案1】:

    使用此委托方法,将 indexPath 设置在第一个位置:

    斯威夫特

    self.collectionView?.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), 
                                                    atScrollPosition: .Top, 
                                                            animated: true)
    

    斯威夫特 3

    self.collectionView?.scrollToItem(at: IndexPath(row: 0, section: 0), 
                                      at: .top, 
                                animated: true)
    

    目标-C

    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] 
                                atScrollPosition:UICollectionViewScrollPositionTop
                                        animated:YES];
    

    如果您想滚动并停在不同的位置,请更改 UICollectionViewScrollPositionTop

    【讨论】:

    • Swift 3 版本:self.collectionView?.scrollToItem(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
    • 这将滚动到第一个可见单元格,而不是 collectionView 中的第一个单元格。
    • @Alex,不,它会滚动到 collectionView 中的第一个单元格。
    【解决方案2】:

    或许可以使用 UIScrollView 相关属性

    collectionView.contentOffset.x = 0
    

    【讨论】:

    • 这个效果最好。它没有动画,但 scrollToItem 不会考虑您拥有的任何边缘插图。
    【解决方案3】:

    您可以将它用于 Objective - C

            [self.restaurantCollectionView setContentOffset:CGPointZero animated:YES];
    

    【讨论】:

      【解决方案4】:

      以下解决方案对我来说很好:

      UIView.animate(withDuration: 0.5, animations: {
           self.collectionView?.contentOffset.x = 0
      })
      

      它滚动到第一个项目,也可以看到 contentInset。

      【讨论】:

        【解决方案5】:
         scrollView.setContentOffset(CGPoint(x: 0.0, y: 0.0), animated: true)
        

        【讨论】:

          【解决方案6】:

          默认 CollectionView 委托...

          - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;
          

          【讨论】:

            【解决方案7】:
            if let indexPath = self.collectionView?.indexPathForItem(at: CGPoint(x: 0, y: 0)) {
                        self.collectionView?.scrollToItem(at: indexPath, at: .top, animated: false)
            }
            

            【讨论】:

              【解决方案8】:

              我发现可以在所有设备上可靠地工作,并且可以正确处理 iPhone X 风格的安全区域。

              let top = CGPoint(x: collectionView.contentOffset.x,
                                y: collectionView.adjustedContentInset.top)
              collectionView.setContentOffset(top, animated: false)
              

              (这会垂直滚动到顶部,但如果需要,您可以轻松地将其滚动到左上角。)

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2016-11-20
                • 1970-01-01
                • 2013-04-05
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多