// // NavigationController.swift // Lookfit // // Created by lemo. on 2020/3/7. // Copyright © 2020 Sheldon. All rights reserved. // import UIKit class NavigationController: UINavigationController { override func viewDidLoad() { super.viewDidLoad() LFLogs("init: \(type(of: self))") makeUI() // 手势代理 self.interactivePopGestureRecognizer?.delegate = self } override var preferredStatusBarStyle: UIStatusBarStyle { let viewController = self.viewControllers.last as? ViewController let isLightStatusBar = viewController?.isLightStatusBar ?? false if #available(iOS 13.0, *) { return isLightStatusBar ? .lightContent : .darkContent } else { return isLightStatusBar ? .lightContent : .default } } func makeUI() { // 设置导航栏样式 navigationBar.setBackgroundImage(UIImage.color(ThemeManager.commonBgColor), for: UIBarPosition.any, barMetrics: .default) navigationBar.shadowImage = UIImage() // 标题样式 let bar = UINavigationBar.appearance() bar.titleTextAttributes = [ NSAttributedString.Key.foregroundColor : ThemeManager.commonTextColor, NSAttributedString.Key.font: ThemeManager.navBarFont] // 分割线获取隐藏 if let backgroundView = navigationBar.subviews.first { backgroundView.subviews.forEach { (view) in if view.height <= 1 { view.isHidden = true } } } if #available(iOS 15, *) { let app = UINavigationBarAppearance.init() app.configureWithOpaqueBackground() // 重置背景和阴影颜色 app.titleTextAttributes = [ NSAttributedString.Key.font: ThemeManager.navBarFont, NSAttributedString.Key.foregroundColor: ThemeManager.commonTextColor ] app.backgroundColor = ThemeManager.commonBgColor // 设置导航栏背景色 app.shadowImage = UIImage() // 设置导航栏下边界分割线透明 navigationBar.scrollEdgeAppearance = app // 带scroll滑动的页面 navigationBar.standardAppearance = app // 常规页面 } } override func pushViewController(_ viewController: UIViewController, animated: Bool) { // 自定义返回按钮 if children.count > 0, let vc = viewController as? ViewController { let leftItems = UIBarButtonItem.baseBarButtonItem(normalImg: "nav_back", highImg: "nav_back", title: vc.navigationTitle, target: self, action: #selector(clickBack)) vc.navigationItem.leftBarButtonItem = leftItems vc.hidesBottomBarWhenPushed = true } super.pushViewController(viewController, animated: animated) } @objc func clickBack() { self.popViewController(animated: true) } deinit { LFLogs("deinit: \(type(of: self))") } } // MARK: - 手势处理 extension NavigationController: UIGestureRecognizerDelegate { /// 判断情况禁用手势 func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { let isRootVC = self.viewControllers.count > 1 return isRootVC } /// 点击屏幕停止编辑 override func touchesBegan(_ touches: Set, with event: UIEvent?) { super.touchesBegan(touches, with: event) view.endEditing(true) } }