// // NewTabBarController.swift // YuMi // // Created by AI on 2025-10-09. // Copyright © 2025 YuMi. All rights reserved. // import UIKit /// 新的 TabBar 控制器 /// 只包含 Moment 和 Mine 两个 Tab class NewTabBarController: UITabBarController { // MARK: - Properties /// 全局事件管理器 private var globalEventManager: GlobalEventManager? /// 是否已登录 private var isLoggedIn: Bool = false // MARK: - Lifecycle override func viewDidLoad() { super.viewDidLoad() // 测试域名配置 #if DEBUG APIConfig.testEncryption() #endif setupTabBarAppearance() setupGlobalManagers() setupInitialViewControllers() NSLog("[NewTabBarController] 初始化完成") } deinit { globalEventManager?.removeAllDelegates() NSLog("[NewTabBarController] 已释放") } // MARK: - Setup /// 设置 TabBar 外观 private func setupTabBarAppearance() { // 自定义 TabBar 样式 tabBar.tintColor = UIColor(red: 0.2, green: 0.6, blue: 0.86, alpha: 1.0) // 新主色调 tabBar.unselectedItemTintColor = UIColor(white: 0.6, alpha: 1.0) // 新辅助色 tabBar.backgroundColor = .white tabBar.isTranslucent = false // 添加顶部分割线 if #available(iOS 13.0, *) { let appearance = UITabBarAppearance() appearance.configureWithOpaqueBackground() appearance.backgroundColor = .white appearance.stackedLayoutAppearance.selected.iconColor = tabBar.tintColor appearance.stackedLayoutAppearance.selected.titleTextAttributes = [ .foregroundColor: tabBar.tintColor ?? .blue, .font: UIFont.systemFont(ofSize: 10, weight: .medium) ] appearance.stackedLayoutAppearance.normal.titleTextAttributes = [ .foregroundColor: tabBar.unselectedItemTintColor ?? .gray, .font: UIFont.systemFont(ofSize: 10) ] tabBar.standardAppearance = appearance if #available(iOS 15.0, *) { tabBar.scrollEdgeAppearance = appearance } } NSLog("[NewTabBarController] TabBar 外观设置完成") } /// 设置全局管理器 private func setupGlobalManagers() { globalEventManager = GlobalEventManager.shared() globalEventManager?.setupSDKDelegates() // 设置房间最小化视图 if let containerView = view { globalEventManager?.setupRoomMiniView(on: containerView) } // 注册社交分享回调 globalEventManager?.registerSocialShareCallback() NSLog("[NewTabBarController] 全局管理器设置完成") } /// 设置初始 ViewController(未登录状态) private func setupInitialViewControllers() { // TODO: 暂时使用空白页面占位 let blankVC1 = UIViewController() blankVC1.view.backgroundColor = .white blankVC1.tabBarItem = createTabBarItem( title: "动态", normalImage: "tab_moment_normal", selectedImage: "tab_moment_selected" ) let blankVC2 = UIViewController() blankVC2.view.backgroundColor = .white blankVC2.tabBarItem = createTabBarItem( title: "我的", normalImage: "tab_mine_normal", selectedImage: "tab_mine_selected" ) viewControllers = [blankVC1, blankVC2] selectedIndex = 0 NSLog("[NewTabBarController] 初始 ViewControllers 设置完成") } /// 创建 TabBarItem /// - Parameters: /// - title: 标题 /// - normalImage: 未选中图标名称 /// - selectedImage: 选中图标名称 /// - Returns: UITabBarItem private func createTabBarItem(title: String, normalImage: String, selectedImage: String) -> UITabBarItem { let item = UITabBarItem( title: title, image: UIImage(named: normalImage)?.withRenderingMode(.alwaysOriginal), selectedImage: UIImage(named: selectedImage)?.withRenderingMode(.alwaysOriginal) ) return item } // MARK: - Public Methods /// 登录成功后刷新 TabBar /// - Parameter isLogin: 是否已登录 @objc func refreshTabBar(isLogin: Bool) { isLoggedIn = isLogin if isLogin { setupLoggedInViewControllers() } else { setupInitialViewControllers() } NSLog("[NewTabBarController] TabBar 已刷新,登录状态: \(isLogin)") } /// 设置登录后的 ViewControllers private func setupLoggedInViewControllers() { // TODO: 等 NewMomentViewController 和 NewMineViewController 创建后替换 // let momentVC = NewMomentViewController() // let mineVC = NewMineViewController() let momentVC = UIViewController() momentVC.view.backgroundColor = .white momentVC.tabBarItem = createTabBarItem( title: "动态", normalImage: "tab_moment_normal", selectedImage: "tab_moment_selected" ) let mineVC = UIViewController() mineVC.view.backgroundColor = .white mineVC.tabBarItem = createTabBarItem( title: "我的", normalImage: "tab_mine_normal", selectedImage: "tab_mine_selected" ) viewControllers = [momentVC, mineVC] selectedIndex = 0 NSLog("[NewTabBarController] 登录后 ViewControllers 设置完成") } } // MARK: - UITabBarControllerDelegate extension NewTabBarController: UITabBarControllerDelegate { override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { NSLog("[NewTabBarController] 选中 Tab: \(item.title ?? "Unknown")") } } // MARK: - OC Compatibility extension NewTabBarController { /// OC 兼容:创建实例的工厂方法 @objc static func create() -> NewTabBarController { return NewTabBarController() } }