Get UIScrollView to scroll to the top

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

How do I make a UIScrollView scroll to the top?

15条回答
  •  佛祖请我去吃肉
    2020-12-07 09:28

    iOS 11 and above

    Try to play around with the new adjustedContentInset (It should even work with prefersLargeTitles, safe area etc.)

    For example (scroll to the top):

    var offset = CGPoint(
        x: -scrollView.contentInset.left, 
        y: -scrollView.contentInset.top)
    
    if #available(iOS 11.0, *) {
        offset = CGPoint(
            x: -scrollView.adjustedContentInset.left, 
            y: -scrollView.adjustedContentInset.top)    
    }
    
    scrollView.setContentOffset(offset, animated: true)
    

提交回复
热议问题