From 0e5261ef9476d18794aceb3401cf45e775bfb597 Mon Sep 17 00:00:00 2001 From: Kyle Bashour Date: Sat, 26 Nov 2022 16:08:52 -0800 Subject: [PATCH] Fix scroll to top --- .../Root/MainTab/MainTabBarController.swift | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Mastodon/Scene/Root/MainTab/MainTabBarController.swift b/Mastodon/Scene/Root/MainTab/MainTabBarController.swift index 2e5d5ae58..a2778b551 100644 --- a/Mastodon/Scene/Root/MainTab/MainTabBarController.swift +++ b/Mastodon/Scene/Root/MainTab/MainTabBarController.swift @@ -569,25 +569,24 @@ extension MainTabBarController: UITabBarControllerDelegate { composeButtonDidPressed(tabBarController) 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 } 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) - defer { - if let tab = Tab(rawValue: viewController.tabBarItem.tag) { - currentTab = tab - } + if let tab = Tab(rawValue: viewController.tabBarItem.tag) { + 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) } }