UICollectionView - Scrolling to first cell

后端 未结 8 1568
一生所求
一生所求 2021-02-12 18:00

I\'m making use of UICollectionView and I need to scroll to first cell after click in a button.

The button is located in a (big) header section in my collection view...

相关标签:
8条回答
  • 2021-02-12 18:04

    You can use this for Objective - C

            [self.restaurantCollectionView setContentOffset:CGPointZero animated:YES];
    
    0 讨论(0)
  • 2021-02-12 18:08
     scrollView.setContentOffset(CGPoint(x: 0.0, y: 0.0), animated: true)
    
    0 讨论(0)
  • 2021-02-12 18:16

    Use this delegate method, setting the indexPath at the first position:

    Swift

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

    Swift 3

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

    Objective-C

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

    Change UICollectionViewScrollPositionTop if you want to scroll and stop at a different position

    0 讨论(0)
  • 2021-02-12 18:17

    It might be possible to use the UIScrollView related property

    collectionView.contentOffset.x = 0
    
    0 讨论(0)
  • 2021-02-12 18:17
    if let indexPath = self.collectionView?.indexPathForItem(at: CGPoint(x: 0, y: 0)) {
                self.collectionView?.scrollToItem(at: indexPath, at: .top, animated: false)
    }
    
    0 讨论(0)
  • 2021-02-12 18:22

    Default CollectionView Delgeate...

    - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;
    
    0 讨论(0)
提交回复
热议问题