UITableView - scroll to the top

前端 未结 30 3101
南方客
南方客 2020-11-28 17:22

In my table view I have to scroll to the top. But I cannot guarantee that the first object is going to be section 0, row 0. May be that my table view will start from section

30条回答
  •  余生分开走
    2020-11-28 18:03

    Here's what I use to work correctly on iOS 11:

    extension UIScrollView {
        func scrollToTop(animated: Bool) {
            var offset = contentOffset
            if #available(iOS 11, *) {
                offset.y = -adjustedContentInset.top
            } else {
                offset.y = -contentInset.top
            }
            setContentOffset(offset, animated: animated)
        }
    }
    

提交回复
热议问题