// // AppCoordinator.swift // Meterage // // Created by 203 on 2023/2/1. // import Foundation import UIKit enum TabBarSelectedType { case home case sample case mine } class AppCoordinator { static let shared = AppCoordinator() private var tabBarController: UITabBarController! private var window: UIWindow? func appDidFinishLaunching(window: UIWindow) { self.window = window window.rootViewController = GuideViewController() } private var homePageController: HomePageViewController = { HomePageViewController() }() private var samplePageController: SamplePageViewController = { SamplePageViewController() }() private var minePageController: MineViewController = { MineViewController() }() func setTabBarViewController() { let home = UINavigationController(rootViewController: homePageController) let sample = UINavigationController(rootViewController: samplePageController) let mine = UINavigationController(rootViewController: minePageController) tabBarController = UITabBarController(nibName: nil, bundle: nil) let navControllers = [home, sample, mine] tabBarController.viewControllers = navControllers for nav in navControllers { nav.navigationBar.barTintColor = .mainThemeColor // main bar color let dict: [NSAttributedString.Key: Any] = [ NSAttributedString.Key.foregroundColor: UIColor.white ] nav.navigationBar.titleTextAttributes = dict // title color nav.navigationBar.tintColor = .white // item color } guard let items = tabBarController.tabBar.items else { return } for (i, m) in zip(items, MenuItemModel.menu) { i.title = m.name i.image = UIImage(named: m.image)?.withRenderingMode(.alwaysOriginal) // 总是显示原图不被颜色覆盖 i.selectedImage = UIImage(named: m.selectedImage)?.withRenderingMode(.alwaysOriginal) } // 改变文字颜色 UITabBarItem.appearance().setTitleTextAttributes( [NSAttributedString.Key.foregroundColor: UIColor.gray], for: .normal ) UITabBarItem.appearance().setTitleTextAttributes( [NSAttributedString.Key.foregroundColor: UIColor.mainThemeColor], for: .selected ) window?.rootViewController = tabBarController } func setupTabBarIndex(index: Int) { tabBarController.selectedIndex = index } func setupTabBar(type: TabBarSelectedType) { switch type { case .home: tabBarController.selectedIndex = 0 case .sample: tabBarController.selectedIndex = 1 case .mine: tabBarController.selectedIndex = 2 } } }