Reset scroll on UICollectionView

前端 未结 9 1308
太阳男子
太阳男子 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: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)
        }
    }
    

提交回复
热议问题