UIViewController+Extension.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // UIViewController+Extension.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by mac on 2024/4/10.
  6. //
  7. import UIKit
  8. // MARK: - 一、基本的扩展
  9. public extension UIViewController {
  10. // MARK: 1.1、pop回上一个界面
  11. /// pop回上一个界面
  12. func popToPreviousVC() {
  13. guard let nav = self.navigationController else { return }
  14. if let index = nav.viewControllers.firstIndex(of: self), index > 0 {
  15. let vc = nav.viewControllers[index - 1]
  16. nav.popToViewController(vc, animated: true)
  17. }
  18. }
  19. // MARK: 1.2、获取push进来的 VC
  20. /// 获取push进来的 VC
  21. /// - Returns: push进来的 VC
  22. func getPreviousNavVC() -> UIViewController? {
  23. guard let nav = self.navigationController else { return nil }
  24. if nav.viewControllers.count <= 1 {
  25. return nil
  26. }
  27. if let index = nav.viewControllers.firstIndex(of: self), index > 0 {
  28. let vc = nav.viewControllers[index - 1]
  29. return vc
  30. }
  31. return nil
  32. }
  33. // MARK: 1.3、获取顶部控制器(类方法)
  34. /// 获取顶部控制器
  35. /// - Returns: VC
  36. static func topViewController() -> UIViewController? {
  37. guard let window = UIApplication.shared.windows.filter({$0.isKeyWindow}).first, let rootVC = window.rootViewController else {
  38. return nil
  39. }
  40. return top(rootVC: rootVC)
  41. }
  42. // MARK: 1.4、获取顶部控制器(实例方法)
  43. /// 获取顶部控制器
  44. /// - Returns: VC
  45. func topViewController() -> UIViewController? {
  46. guard let window = UIApplication.shared.windows.filter({$0.isKeyWindow}).first, let rootVC = window.rootViewController else {
  47. return nil
  48. }
  49. return Self.top(rootVC: rootVC)
  50. }
  51. private static func top(rootVC: UIViewController?) -> UIViewController? {
  52. if let presentedVC = rootVC?.presentedViewController {
  53. return top(rootVC: presentedVC)
  54. }
  55. if let nav = rootVC as? UINavigationController,
  56. let lastVC = nav.viewControllers.last {
  57. return top(rootVC: lastVC)
  58. }
  59. if let tab = rootVC as? UITabBarController,
  60. let selectedVC = tab.selectedViewController {
  61. return top(rootVC: selectedVC)
  62. }
  63. return rootVC
  64. }
  65. // MARK: 1.5、是否正在展示
  66. /// 是否正在展示
  67. var isCurrentVC: Bool {
  68. return isViewLoaded == true && (view!.window != nil)
  69. }
  70. // MARK: 1.6、关闭当前的控制器
  71. /// 关闭当前的控制器
  72. func closeCurrentVC() {
  73. guard let nav = self.navigationController else {
  74. dismiss(animated: true, completion: nil)
  75. return
  76. }
  77. if nav.viewControllers.count > 1 {
  78. nav.popViewController(animated: true)
  79. } else if let _ = nav.presentingViewController {
  80. nav.dismiss(animated: true, completion: nil)
  81. }
  82. }
  83. //MARK: 1.7、dismiss到某个vc
  84. /// dismiss到某个vc
  85. /// - Parameters:
  86. /// - vc: vc
  87. /// - animated: 是否需要动画
  88. @discardableResult
  89. func dismiss(vc: AnyClass, animated: Bool) -> Bool {
  90. guard let targetPresentingVC = findPresentingViewController(fromVc: self, toVc: vc) else { return false }
  91. targetPresentingVC.dismiss(animated: animated)
  92. return true
  93. }
  94. //MARK: 寻找到对应的presentingViewController
  95. /// 寻找到对应的presentingViewController
  96. /// - Parameters:
  97. /// - fromVc: 当前vc
  98. /// - toVc: 目标vc
  99. /// - Returns: 目标presentingViewController
  100. private func findPresentingViewController(fromVc: UIViewController, toVc: AnyClass) -> UIViewController? {
  101. // 判断是否存在对应的presentingViewController
  102. guard let vc = fromVc.presentingViewController else { return nil }
  103. // 判断是否是目标vc
  104. if (vc.isMember(of: toVc)) {
  105. // 寻找到对应的presentingViewController
  106. return vc
  107. } else {
  108. return findPresentingViewController(fromVc: vc, toVc: toVc)
  109. }
  110. }
  111. func pushVC(vc: UIViewController ) {
  112. if vc.isKind(of: UIViewController.self) == false {
  113. return
  114. }
  115. if self.navigationController == nil {
  116. return
  117. }
  118. if vc.hidesBottomBarWhenPushed == false {
  119. vc.hidesBottomBarWhenPushed = true
  120. }
  121. self.navigationController?.pushViewController(vc, animated: true)
  122. }
  123. }