BaseNavigationController.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // BaseNavigationController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2020/12/28.
  6. // Copyright © 2020 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. /// 导航栏视图容器控制类基类
  10. class BaseNavigationController: UINavigationController, UINavigationControllerDelegate {
  11. // MARK: - 生命周期
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. // Do any additional setup after loading the view.
  15. if #available(iOS 13.0, *) {
  16. let appearance = UINavigationBarAppearance()
  17. appearance.configureWithOpaqueBackground()
  18. appearance.backgroundColor = .fromHex(rgbValue: 0x2481DD)
  19. appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont.font_18]
  20. navigationBar.standardAppearance = appearance
  21. navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
  22. } else {
  23. // Fallback on earlier versions
  24. UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont.font_18]
  25. UINavigationBar.appearance().barTintColor = .fromHex(rgbValue: 0x2481DD)
  26. }
  27. UIBarButtonItem.appearance().tintColor = .white
  28. /// 侧边栏滑动手势
  29. self.interactivePopGestureRecognizer?.delegate = self
  30. self.delegate = self
  31. }
  32. // MARK: - 返回手势相关
  33. override func pushViewController(_ viewController: UIViewController, animated: Bool) {
  34. if self.viewControllers.count >= 1 {
  35. viewController.hidesBottomBarWhenPushed = true
  36. viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .done, target: self, action: #selector(backToParent))
  37. }
  38. super.pushViewController(viewController, animated: animated)
  39. }
  40. @objc func backToParent() {
  41. self.popViewController(animated: true)
  42. }
  43. }
  44. extension BaseNavigationController: UIGestureRecognizerDelegate {
  45. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  46. return self.children.count > 1
  47. }
  48. }