Merge pull request #701 from kylebshr/kb/scroll-to-top

Fix scroll to top when tab is tapped
This commit is contained in:
Marcus Kida 2022-11-28 16:08:18 +01:00 committed by GitHub
commit a790199ee4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 13 deletions

View File

@ -569,25 +569,24 @@ extension MainTabBarController: UITabBarControllerDelegate {
composeButtonDidPressed(tabBarController) composeButtonDidPressed(tabBarController)
return false return false
} }
// Assert index is as same as the tab rawValue. This check needs to be done `shouldSelect`
// because the nav controller has already popped in `didSelect`.
if currentTab.rawValue == tabBarController.selectedIndex,
let navigationController = viewController as? UINavigationController,
navigationController.viewControllers.count == 1,
let scrollViewContainer = navigationController.topViewController as? ScrollViewContainer {
scrollViewContainer.scrollToTop(animated: true)
}
return true return true
} }
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) { func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: select %s", ((#file as NSString).lastPathComponent), #line, #function, viewController.debugDescription) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: select %s", ((#file as NSString).lastPathComponent), #line, #function, viewController.debugDescription)
defer { if let tab = Tab(rawValue: viewController.tabBarItem.tag) {
if let tab = Tab(rawValue: viewController.tabBarItem.tag) { currentTab = tab
currentTab = tab
}
} }
// assert index is as same as the tab rawValue
guard currentTab.rawValue == tabBarController.selectedIndex,
let navigationController = viewController as? UINavigationController,
navigationController.viewControllers.count == 1,
let scrollViewContainer = navigationController.topViewController as? ScrollViewContainer else {
return
}
scrollViewContainer.scrollToTop(animated: true)
} }
} }