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
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)
}
}
}