feat: scroll to top if needs when tap tab

This commit is contained in:
CMK 2021-06-24 19:58:17 +08:00
parent 6da2d87cf6
commit b3c36c3126
1 changed files with 30 additions and 2 deletions

View File

@ -16,6 +16,8 @@ class MainTabBarController: UITabBarController {
weak var context: AppContext!
weak var coordinator: SceneCoordinator!
var currentTab = Tab.home
enum Tab: Int, CaseIterable {
case home
@ -95,7 +97,9 @@ extension MainTabBarController {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
view.backgroundColor = .systemBackground
let tabs = Tab.allCases
@ -196,6 +200,27 @@ extension MainTabBarController {
}
// MARK: - UITabBarControllerDelegate
extension MainTabBarController: UITabBarControllerDelegate {
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: tabBarController.selectedIndex) {
currentTab = tab
}
}
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)
}
}
// HIG: keyboard UX
// https://developer.apple.com/design/human-interface-guidelines/macos/user-interaction/keyboard/
extension MainTabBarController {
@ -307,7 +332,10 @@ extension MainTabBarController {
guard let index = Tab.allCases.firstIndex(of: tab) else { return }
let previousTab = Tab(rawValue: selectedIndex)
selectedIndex = index
if let tab = Tab(rawValue: index) {
currentTab = tab
}
if let previousTab = previousTab {
switch (tab, previousTab) {
case (.home, .home):