// // HomeTabBarController.swift // Lookfit // // Created by lemo. on 2020/3/7. // Copyright © 2020 Sheldon. All rights reserved. // import UIKit enum HomeTabBarItem: Int { case health, sport, device, mine private func controller(with viewModel: ViewModel, navigator: Navigator) -> UIViewController { switch self { case .health: // let vc = HealthViewController(viewModel: viewModel, navigator: navigator) let vc = HealthHomeViewControllers(viewModel: viewModel, navigator: navigator) vc.isHiddenNavBar = true return NavigationController(rootViewController: vc) case .sport: let vc = SportViewControllers(viewModel: viewModel, navigator: navigator) return NavigationController(rootViewController: vc) case .device: let vc = DeviceViewControllers(viewModel: viewModel, navigator: navigator) // vc.tongzhi(); 加入的目的是什么?初始化就已经跑了? 加入会导致重复执行通知 return NavigationController(rootViewController: vc) case .mine: let vc = MineViewController(viewModel: viewModel, navigator: navigator) vc.isHiddenNavBar = true vc.isLightStatusBar = true return NavigationController(rootViewController: vc) } } var image: UIImage? { switch self { case .health: return R.image.icon_health_normal() case .sport: return R.image.icon_sport_normal() case .device: return R.image.icon_device_normal() case .mine: return R.image.icon_mine_normal() } } var selectedImage: UIImage? { switch self { case .health: return R.image.icon_health_selected() case .sport: return R.image.icon_sport_selected() case .device: return R.image.icon_device_selected() case .mine: return R.image.icon_mine_selected() } } var title: String { switch self { case .health: return MultiLanguageKey.health.localized case .sport: return MultiLanguageKey.sport.localized case .device: return MultiLanguageKey.device.localized case .mine: return MultiLanguageKey.mine.localized } } func getController(with viewModel: ViewModel, navigator: Navigator) -> UIViewController { let vc = controller(with: viewModel, navigator: navigator) let item = UITabBarItem(title: title, image: image?.withRenderingMode(.alwaysOriginal), selectedImage: selectedImage?.withRenderingMode(.alwaysOriginal)).then { $0.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: kHexColor(0x2A2A2A)], for: .normal) $0.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: kHexColor(0x004EFF)], for: .selected) } vc.tabBarItem = item return vc } } class HomeTabBarController: UITabBarController, Navigatable { var viewModel: HomeTabBarViewModel? var navigator: Navigator! init(viewModel: ViewModel?, navigator: Navigator) { self.viewModel = viewModel as? HomeTabBarViewModel self.navigator = navigator super.init(nibName: nil, bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() makeUI() bindViewModel() } func makeUI() { delegate = self tabBar.isTranslucent = false tabBar.barTintColor = ThemeManager.tabBarColor tabBar.backgroundImage = UIImage() tabBar.shadowImage = UIImage() tabBar.layer.shadowColor = UIColor.lightGray.cgColor tabBar.layer.shadowOffset = CGSize(width: 0, height: -5) tabBar.layer.shadowOpacity = 0.3 if #available(iOS 13, *) { let lineView = UIView(frame: CGRect(x: 0, y: -1, width: tabBar.bounds.width, height: 1)) lineView.backgroundColor = ThemeManager.tabBarColor tabBar.addSubview(lineView) } if #available(iOS 15, *) { let bar = UITabBarAppearance.init() bar.backgroundColor = ThemeManager.tabBarColor bar.shadowImage = UIImage() let selTitleAttr = [ // NSAttributedString.Key.font: itemFont, NSAttributedString.Key.foregroundColor: kHexColor(0x004EFF) ] bar.stackedLayoutAppearance.selected.titleTextAttributes = selTitleAttr self.tabBar.scrollEdgeAppearance = bar self.tabBar.standardAppearance = bar } } func bindViewModel() { guard let viewModel = viewModel else { return } let controllers = viewModel.tabBarItems.map { $0.getController(with: viewModel.viewModel(for: $0), navigator: navigator) } setViewControllers(controllers, animated: false) } } extension HomeTabBarController: UITabBarControllerDelegate { func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { return true } func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) { } }