I am curious when the UITableView\'s content size is updated after doing an insert/delete animation call. I figured it would be like most [UIView animation...] blocks in tha
It looks like sizeThatFits()
can expose the height of a new contentSize
that's pending. You can then assign that pending size to have it resolve early for scroll animations.
With something like this:
extension UIScrollView {
var pendingContentSize: CGSize {
var tallSize = contentSize
tallSize.height = .greatestFiniteMagnitude
return sizeThatFits(tallSize)
}
func scrollToBottom(animated: Bool) {
contentSize = pendingContentSize
let contentRect = CGRect(origin: .zero, size: contentSize)
let (bottomSlice, _) = contentRect.divided(atDistance: 1, from: .maxYEdge)
guard !bottomSlice.isEmpty else { return }
scrollRectToVisible(bottomSlice, animated: animated)
}
}
I'm able to write view controller code like this:
tableView.insertRows(at: [newIndexPath], with: .none)
tableView.scrollToBottom(animated: true)
and have the table scroll all the way to the bottom (using the new content size) instead of it scrolling down to the 2nd-to-last row (using the old content size).