Reset scroll on UICollectionView

前端 未结 9 1187
太阳男子
太阳男子 2021-01-30 05:25

I have a horizontal UICollectionView which works fine and scrolls. When I tap an item I update my data and call reloadData. This works and the new data

相关标签:
9条回答
  • 2021-01-30 05:30

    You can use this method to scroll to any item you want:

    - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath 
                   atScrollPosition:(UICollectionViewScrollPosition)scrollPosition 
                           animated:(BOOL)animated
    
    0 讨论(0)
  • 2021-01-30 05:31

    If you view controller contains safe area, code:

    collectionView.setContentOffset(CGPointZero, animated: true)
    

    doesn't work, so you can use universal extension:

    extension UIScrollView {
        func scrollToTop(_ animated: Bool) {
            var topContentOffset: CGPoint
            if #available(iOS 11.0, *) {
                topContentOffset = CGPoint(x: -safeAreaInsets.left, y: -safeAreaInsets.top)
            } else {
                topContentOffset = CGPoint(x: -contentInset.left, y: -contentInset.top)
            }
            setContentOffset(topContentOffset, animated: animated)
        }
    }
    
    0 讨论(0)
  • 2021-01-30 05:34

    In Swift:

    collectionView.setContentOffset(CGPointZero, animated: true)
    

    If you leave the view that houses the collection view, make a change in the 2nd view controller, and need the collection view to update upon returning:

    @IBAction func unwindTo_CollectionViewVC(segue: UIStoryboardSegue) {
        //Automatic table reload upon unwind
        viewDidLoad()
        collectionView.reloadData()
        collectionView.setContentOffset(CGPointZero, animated: true)
    }
    

    In my situation the unwind is great because the viewDidLoad call will update the CoreData context, the reloadData call will make sure the collectionView updates to reflect the new CoreData context, and then the contentOffset will make sure the table sets back to the top.

    0 讨论(0)
  • 2021-01-30 05:35
    • SWIFT 4.2 SOLUTION- Say I have a tableView with each tableViewCell containing a HorizontalCollectionView Due to reuse of cells, even though first tableViewCell is scrolled to say page 4, downwards, other cell is also set to page 4.

    This works in such cases- In the cellForRowAt func in tableView, let cell = //declaring a cell using deque cell.myCollectionView.contentOffset = CGPoint(x:0,y:0)

    0 讨论(0)
  • 2021-01-30 05:36

    Sorry I couldn't comment at the time of this writing, but I was using a UINavigationController and CGPointZero itself didn't get me to the very top so I had to use the following instead.

    CGFloat compensateHeight = -(self.navigationController.navigationBar.bounds.size.height+[UIApplication sharedApplication].statusBarFrame.size.height);
    [self.collectionView setContentOffset:CGPointMake(0, compensateHeight) animated:YES];
    

    Hope this helps somebody in future. Cheers!

    0 讨论(0)
  • 2021-01-30 05:37

    To deal with an UINavigationController and a transparent navigation bar, I had to calculate the extra top offset to perfectly match the position of the first element.

    Swift 1.1 code:

         let topOffest = CGPointMake(0, -(self.collectionView?.contentInset.top ?? O))
         self.collectionView?.setContentOffset(topOffest, animated: true)
    

    Cheers!

    0 讨论(0)
提交回复
热议问题