Reset scroll on UICollectionView

前端 未结 9 1199
太阳男子
太阳男子 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:46

    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()
    

提交回复
热议问题