// // UIViewController+Extension.swift // DLSDK // // Created by osell on 2023/8/2. // import Foundation import UIKit import URLNavigator extension UIViewController { /// 获取当前显示的 View Controller public static var topViewController: UIViewController? { var keyWindow = UIApplication.keyWindow if keyWindow == nil { for window in UIApplication.shared.windows { if window.isKeyWindow { keyWindow = window break } } } var vc = keyWindow?.rootViewController while true { if let nc = vc as? UINavigationController { vc = nc.visibleViewController } else if let tbc = vc as? UITabBarController { if let svc = tbc.selectedViewController { vc = svc } else { break } } else if let pvc = vc?.presentedViewController { vc = pvc } else { break } } return vc } } public extension UIViewController { struct Key { static var needLogin = "needLoginKey" } /// 是否需要登录 var isNeedLogin: Bool { get { objc_getAssociatedObject(self, &Key.needLogin) as? Bool ?? false } set { objc_setAssociatedObject(self, &Key.needLogin, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } } } // MARK: Storyboard extension UIViewController { /// 从 Storyboard 中获取 ViewController public static func viewControllerWithIdentifier(_ id: String, storyboardName name: String) -> UIViewController { let storyboard = UIStoryboard(name: name, bundle: nil) return storyboard.instantiateViewController(withIdentifier: id) } } extension UIViewController { class func currentViewController(base: UIViewController? = UIApplication.keyWindow?.rootViewController) -> UIViewController? { var base = base if base == nil { for window in UIApplication.shared.windows { if window.isKeyWindow { base = window.rootViewController break } } } if let nav = base as? UINavigationController { return currentViewController(base: nav.visibleViewController) } if let tab = base as? UITabBarController { return currentViewController(base: tab.selectedViewController) } if let presented = base?.presentedViewController { return currentViewController(base: presented) } return base } } // MARK: - Toast extension UIViewController { static func showErrorToast(message: String) { #if !SHARE_EXTENSION // 查询当前ViewController是否存在HUD,存在则在隐藏 if let topMostVC = UIViewController.topMost, ProgressHUD.findProgressHUD(for: topMostVC.view) != nil { ProgressHUD.dismiss(immediate: true, forController: topMostVC) } if message != "" { ProgressHUD.showError(withStatus: message) } else { ProgressHUD.dismiss() } #endif } } // MARK: 导航 extension UIViewController { func addNavBack() { let backBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 44)) backBtn.setImage(UIImage(named: "navBack"), for: .normal) backBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: -15, bottom: 0, right: 0) backBtn.addTarget(self, action: #selector(back), for: .touchUpInside) let leftItem = UIBarButtonItem(customView: backBtn) self.navigationItem.leftBarButtonItem = leftItem // self.navigationItem.leftMargin = 0 } @objc func back() { self.navigationController?.popViewController(animated: true) } /// 显示 view controller(根据当前上下文,自动选择 push 或 present 方式) @objc public class func showViewController(_ controller: UIViewController, animated flag: Bool) { let topViewController = UIViewController.topViewController if let navigationController = topViewController as? UINavigationController { navigationController.pushViewController(controller, animated: flag) } else if let navigationController = topViewController?.navigationController { navigationController.pushViewController(controller, animated: flag) } else { topViewController?.present(controller, animated: flag, completion: nil) } } public static func presentController(_ controller: UIViewController, animated flag: Bool) { let topViewController = UIViewController.topViewController topViewController?.present(controller, animated: flag, completion: nil) } /// 显示 view controller(根据当前上下文,自动选择 push 或 present 方式) public func showViewControllerAnimated(_ animated: Bool) { UIViewController.showViewController(self, animated: animated) } /// 关闭 view controller(根据当前上下文,自动选择 pop 或 dismiss 方式) public static func closeViewControllerAnimated(_ animated: Bool) { UIViewController.topViewController?.closeViewControllerAnimated(animated) } /// 关闭 view controller(根据当前上下文,自动选择 pop 或 dismiss 方式) public func closeViewControllerAnimated(_ animated: Bool) { if let controller = navigationController, controller.viewControllers.count > 1 { controller.popViewController(animated: animated) } else { dismiss(animated: animated, completion: nil) } } }