How to scroll to the exact end of the UITableView?

前端 未结 16 2510
天涯浪人
天涯浪人 2020-12-07 18:48

I have a UITableView that is populated with cells with dynamic height. I would like the table to scroll to the bottom when the view controller is pushed from vi

16条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 19:24

    To scroll to the end of your TableView you can use the following function, which also works for ScrollViews.

    It also calculates the safe area on the bottom for iPhone X and newer. The call is made from the main queue, to calculate the height correctly.

    func scrollToBottom(animated: Bool) {
        DispatchQueue.main.async {
            let bottomOffset = CGPoint(x: 0, y: self.contentSize.height - self.bounds.size.height + self.safeAreaBottom)
            
            if bottomOffset.y > 0 {
                self.setContentOffset(bottomOffset, animated: animated)
            }
        }
    }
    

提交回复
热议问题