如何使UIScrollView滚动到顶部?
发布于 2012-02-26 11:40:30
iOS 7更新
[self.scrollView setContentOffset:
    CGPointMake(0, -self.scrollView.contentInset.top) animated:YES];原创
[self.scrollView setContentOffset:CGPointZero animated:YES];或者,如果您想保留水平滚动位置而仅重置垂直位置:
[self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x, 0)
    animated:YES];发布于 2016-02-01 08:37:01
下面是一个Swift扩展,它可以让这一切变得简单:
extension UIScrollView {
    func scrollToTop() {
        let desiredOffset = CGPoint(x: 0, y: -contentInset.top)
        setContentOffset(desiredOffset, animated: true)
   }
}用法:
myScrollView.scrollToTop()发布于 2018-02-12 18:55:41
对于Swift 4
scrollView.setContentOffset(.zero, animated: true)https://stackoverflow.com/questions/9450302
复制相似问题