jsdw_ios/QuickLocation/Main/Tabbar/MainTabBarController.swift

97 lines
3.5 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: 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()
}
}