Improve position maintenance

This commit is contained in:
Justin Mazzocchi 2021-01-16 18:47:43 -08:00
parent d42642c079
commit a672e97441
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C

View file

@ -275,22 +275,21 @@ private extension TableViewController {
} }
func update(_ update: CollectionUpdate) { func update(_ update: CollectionUpdate) {
var offsetFromNavigationBar: CGFloat? let positionMaintenanceOffset: CGFloat
if if let itemId = update.maintainScrollPositionItemId,
let itemId = update.maintainScrollPositionItemId, let indexPath = dataSource.indexPath(itemId: itemId) {
let indexPath = dataSource.indexPath(itemId: itemId), positionMaintenanceOffset = tableView.rectForRow(at: indexPath).origin.y
let navigationBar = navigationController?.navigationBar { - tableView.safeAreaInsets.top - tableView.contentOffset.y
let navigationBarMaxY = tableView.convert(navigationBar.bounds, from: navigationBar).maxY } else {
offsetFromNavigationBar = tableView.rectForRow(at: indexPath).origin.y - navigationBarMaxY positionMaintenanceOffset = 0
} }
self.dataSource.apply(update.items.snapshot(), animatingDifferences: false) { [weak self] in self.dataSource.apply(update.items.snapshot(), animatingDifferences: false) { [weak self] in
guard let self = self else { return } guard let self = self else { return }
if if let itemId = update.maintainScrollPositionItemId,
let itemId = update.maintainScrollPositionItemId, let indexPath = self.dataSource.indexPath(itemId: itemId) {
let indexPath = self.dataSource.indexPath(itemId: itemId) {
if update.shouldAdjustContentInset { if update.shouldAdjustContentInset {
self.tableView.contentInset.bottom = max( self.tableView.contentInset.bottom = max(
self.tableView.safeAreaLayoutGuide.layoutFrame.height self.tableView.safeAreaLayoutGuide.layoutFrame.height
@ -299,10 +298,7 @@ private extension TableViewController {
} }
self.tableView.scrollToRow(at: indexPath, at: .top, animated: false) self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
self.tableView.contentOffset.y -= positionMaintenanceOffset
if let offsetFromNavigationBar = offsetFromNavigationBar {
self.tableView.contentOffset.y -= offsetFromNavigationBar
}
} }
} }
} }