Get UIScrollView to scroll to the top

前端 未结 15 1871
眼角桃花
眼角桃花 2020-12-07 08:41

How do I make a UIScrollView scroll to the top?

15条回答
  •  遥遥无期
    2020-12-07 09:25

    Here is a Swift extension that makes it easy:

    extension UIScrollView {
        func scrollToTop() {
            let desiredOffset = CGPoint(x: 0, y: -contentInset.top)
            setContentOffset(desiredOffset, animated: true)
       }
    }
    

    Usage:

    myScrollView.scrollToTop()
    

提交回复
热议问题