jsdw_ios/QuickLocation/Main/Tabbar/MainTabBarController.swift

90 lines
3.2 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: ViewController())
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)
}
}
// 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) {
customTabBar.isHidden = navigationController.viewControllers.count > 1
}
}