jsdw_ios/QuickLocation/Main/Tabbar/MainTabBarController.swift

119 lines
4.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MainTabBarController.swift
// QuickLocation
//
import UIKit
final class MainTabBarController: UITabBarController {
// MARK: - View Controllers
private lazy var locationNav = BaseNavigationController(rootViewController: HomeViewController())
private lazy var exploreNav = BaseNavigationController(rootViewController: FeatureIntroVC())
private lazy var circlesNav = BaseNavigationController(rootViewController: GroupViewController())
private lazy var mineNav = BaseNavigationController(rootViewController: MineViewController())
private lazy var navControllers: [BaseNavigationController] = {
[locationNav, circlesNav, exploreNav, mineNav]
}()
// MARK: - Custom Tab Bar
private let customTabBar: QuickLocationTabBar = {
let items: [QuickLocationTabBar.TabItem] = [
QuickLocationTabBar.TabItem(
title: "位置",
image: UIImage(named: "Tabbar/location"),
selectedImage: UIImage(named: "Tabbar/location_selected")
),
QuickLocationTabBar.TabItem(
title: "圈子",
image: UIImage(named: "Tabbar/group"),
selectedImage: UIImage(named: "Tabbar/group_selected")
),
QuickLocationTabBar.TabItem(
title: "探索",
image: UIImage(named: "Tabbar/explore"),
selectedImage: UIImage(named: "Tabbar/explore_selected")
),
QuickLocationTabBar.TabItem(
title: "我的",
image: UIImage(named: "Tabbar/mine"),
selectedImage: UIImage(named: "Tabbar/mine_selected")
)
]
return QuickLocationTabBar(items: items)
}()
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
viewControllers = navControllers
tabBar.isHidden = true
tabBar.removeFromSuperview()
for nav in navControllers {
nav.delegate = self
}
view.addSubview(customTabBar)
customTabBar.delegate = self
// 点击"探索"(index=1)或"圈子"(index=2)时游客跳登录、不选中
customTabBar.shouldSelectTab = { index in
if index == 1, AppContextManager.shared.isGuest {
AppRouter.push(Route.login)
return false
}
return true
}
customTabBar.translatesAutoresizingMaskIntoConstraints = false
let tabBarHeight = customTabBar.intrinsicContentSize.height
NSLayoutConstraint.activate([
customTabBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
customTabBar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
customTabBar.bottomAnchor.constraint(equalTo: view.bottomAnchor),
customTabBar.heightAnchor.constraint(equalToConstant: tabBarHeight)
])
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
view.bringSubviewToFront(customTabBar)
}
/// 更新 tabBar 可见性:当前 tab 的导航栈只有根 VC 时才显示
/// 在 parent VC 的 viewWillAppear 中回调,比 UINavigationControllerDelegate 更可靠
func updateTabBarVisibility() {
guard let nav = selectedViewController as? UINavigationController else { return }
customTabBar.isHidden = nav.viewControllers.count > 1
}
/// 强制显示/隐藏 tabBar(供底部面板使用)
func setTabBarHidden(_ hidden: Bool) {
customTabBar.isHidden = hidden
}
/// 切换自定义 Tab(需同步 customTabBar 选中态与 UITabBarController.selectedIndex)
func selectTab(at index: Int) {
guard index >= 0, index < navControllers.count else { return }
customTabBar.setSelectedIndex(index)
selectedIndex = index
updateTabBarVisibility()
}
}
// MARK: - QuickLocationTabBarDelegate
extension MainTabBarController: QuickLocationTabBarDelegate {
func tabBar(_ tabBar: QuickLocationTabBar, didSelectTabAt index: Int) {
selectedIndex = index
}
}
// MARK: - UINavigationControllerDelegate
extension MainTabBarController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
updateTabBarVisibility()
}
}