Reset scroll on UICollectionView

2019-03-07 23:06发布

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 is displayed in the UICollectionView. The problem is the scroll position doesn't change and it is still viewing the last place. I want to reset the scrolling to the top (Or left in my case). How can I do this?

8条回答
仙女界的扛把子
2楼-- · 2019-03-07 23:46

You want setContentOffset:. It takes a CGPoint as and argument that you can set to what ever you want using CGPointMake, but if you wish to return to the very beginning of the collection, you can simply use CGPointZero.

[collectionView setContentOffset:CGPointZero animated:YES];
查看更多
Root(大扎)
3楼-- · 2019-03-07 23:46

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)
    }
}
查看更多
贼婆χ
4楼-- · 2019-03-07 23:50

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!

查看更多
可以哭但决不认输i
5楼-- · 2019-03-07 23:51

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.

查看更多
狗以群分
6楼-- · 2019-03-07 23:53

I use this quite often in different parts of my app so I just extended UIScrollView so it can be used on any scroll view and scroll view subclass:

extension UIScrollView {        
    /// Sets content offset to the top.
    func resetScrollPositionToTop() {
        self.contentOffset = CGPoint(x: -contentInset.left, y: -contentInset.top)
    }
}

So whenever I need to reset the position:

self.collectionView.resetScrollPositionToTop()
查看更多
不美不萌又怎样
7楼-- · 2019-03-07 23:55

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!

查看更多
登录 后发表回答