74 lines
2.5 KiB
Swift
74 lines
2.5 KiB
Swift
//
|
|
// TabNavigationController.swift
|
|
// suixinkan
|
|
//
|
|
|
|
import UIKit
|
|
|
|
/// 单个 Tab 的导航栈。
|
|
final class TabNavigationController: UINavigationController {
|
|
|
|
let appTab: AppTab
|
|
|
|
init(tab: AppTab) {
|
|
self.appTab = tab
|
|
super.init(nibName: nil, bundle: nil)
|
|
delegate = self
|
|
navigationBar.prefersLargeTitles = false
|
|
setViewControllers([tab.makeRootViewController()], animated: false)
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
|
|
viewController.hidesBottomBarWhenPushed = viewControllers.count > 0
|
|
updateNavigationBarVisibility(for: viewController, animated: animated)
|
|
super.pushViewController(viewController, animated: animated)
|
|
}
|
|
|
|
override func setViewControllers(_ viewControllers: [UIViewController], animated: Bool) {
|
|
if let topViewController = viewControllers.last {
|
|
updateNavigationBarVisibility(for: topViewController, animated: animated)
|
|
}
|
|
super.setViewControllers(viewControllers, animated: animated)
|
|
}
|
|
|
|
override func popViewController(animated: Bool) -> UIViewController? {
|
|
let poppedViewController = super.popViewController(animated: animated)
|
|
if !animated, let topViewController {
|
|
updateNavigationBarVisibility(for: topViewController, animated: false)
|
|
}
|
|
return poppedViewController
|
|
}
|
|
|
|
override func popToRootViewController(animated: Bool) -> [UIViewController]? {
|
|
let poppedViewControllers = super.popToRootViewController(animated: animated)
|
|
if !animated, let topViewController {
|
|
updateNavigationBarVisibility(for: topViewController, animated: false)
|
|
}
|
|
return poppedViewControllers
|
|
}
|
|
|
|
private func shouldHideNavigationBar(for viewController: UIViewController) -> Bool {
|
|
viewController is HomeViewController
|
|
}
|
|
|
|
private func updateNavigationBarVisibility(for viewController: UIViewController, animated: Bool) {
|
|
setNavigationBarHidden(shouldHideNavigationBar(for: viewController), animated: animated)
|
|
}
|
|
}
|
|
|
|
extension TabNavigationController: UINavigationControllerDelegate {
|
|
|
|
func navigationController(
|
|
_ navigationController: UINavigationController,
|
|
willShow viewController: UIViewController,
|
|
animated: Bool
|
|
) {
|
|
updateNavigationBarVisibility(for: viewController, animated: animated)
|
|
}
|
|
}
|