97 lines
3.5 KiB
Swift
97 lines
3.5 KiB
Swift
//
|
||
// 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: ViewController())
|
||
private lazy var circlesNav = BaseNavigationController(rootViewController: GroupViewController())
|
||
private lazy var mineNav = BaseNavigationController(rootViewController: MineViewController())
|
||
|
||
private lazy var navControllers: [BaseNavigationController] = {
|
||
[locationNav, exploreNav, circlesNav, 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/explore"),
|
||
selectedImage: UIImage(named: "Tabbar/explore_selected")
|
||
),
|
||
QuickLocationTabBar.TabItem(
|
||
title: "圈子",
|
||
image: UIImage(named: "Tabbar/group"),
|
||
selectedImage: UIImage(named: "Tabbar/group_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
|
||
|
||
for nav in navControllers {
|
||
nav.delegate = self
|
||
}
|
||
|
||
view.addSubview(customTabBar)
|
||
customTabBar.delegate = self
|
||
|
||
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, constant: -41),
|
||
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
|
||
}
|
||
}
|
||
|
||
// 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()
|
||
}
|
||
}
|