// // MPNavigationController.swift // MusicPlayer // // Created by Mr.Zhou on 2024/3/27. // import UIKit ///通用导航栏 class MPNavigationController: UINavigationController { override init(rootViewController: UIViewController) { super.init(rootViewController: rootViewController) if #available(iOS 15, *) { let navBar = UINavigationBarAppearance() navBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 20, weight: .heavy)] navBar.backgroundEffect = nil navBar.shadowColor = nil //消除导航栏分割线 navBar.configureWithTransparentBackground() navBar.backgroundColor = .clear rootViewController.navigationController?.navigationBar.scrollEdgeAppearance = navBar rootViewController.navigationController?.navigationBar.standardAppearance = navBar } rootViewController.navigationController?.navigationBar.isTranslucent = false rootViewController.navigationController?.navigationBar.shadowImage = UIImage() //自定义导航栏颜色 rootViewController.navigationController?.navigationBar.barTintColor = .clear //设置导航栏标题颜色 rootViewController.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 20, weight: .heavy)] rootViewController.navigationController?.navigationBar.tintColor = .white let item = UIBarButtonItem(title: "", style: .plain, target: self, action: nil) item.tintColor = .white rootViewController.navigationItem.backBarButtonItem = item } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override init(nibName nibNameorNil:String?,bundle nibBundleOrNil:Bundle?){ super.init(nibName:nibNameorNil,bundle:nibBundleOrNil) } //重写pushViewController //每一次push都会执行这个方法,push之前设置viewController的hidesBottomBarWhenPushed override func pushViewController(_ viewController: UIViewController, animated: Bool) { viewController.hidesBottomBarWhenPushed = true NotificationCenter.notificationKey.post(notificationName: .hidden_show) super.pushViewController(viewController, animated: true) viewController.hidesBottomBarWhenPushed = false } //重写popViewController //每一次对viewController进行push的时候,会把viewController放入一个栈中 //每一次对viewController进行pop的时候,会把viewController从栈中移除 override func popViewController(animated: Bool) -> UIViewController? { guard self.children.count != 1 else { //特殊情况,当用户连续触发pop事件时进行容错处理 return super.popViewController(animated: true) } guard self.children.count == 2 else { //如果viewController栈中存在的ViewController的个数超过两个,对要返回到的上一级的界面设置hidesBottomBarWhenPushed = true //把tabbar进行隐藏 let count = self.children.count-2 let controller = self.children[count] controller.hidesBottomBarWhenPushed = true NotificationCenter.notificationKey.post(notificationName: .hidden_show) return super.popViewController(animated: true) } //如果viewController栈中存在的ViewController的个数为两个,再返回上一级界面就是根界面了 //那么要对tabbar进行显示 let controller:UIViewController = self.children[0] controller.hidesBottomBarWhenPushed = false NotificationCenter.notificationKey.post(notificationName: .display_show) return super.popViewController(animated: true) } }