present之后重新设置rootvc会出现的问题解决
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| static func presentLogin() { let nav = UINavigationController.init(rootViewController: LSLoginViewController()) if let vc = getTopVC() { var parentVC = vc.presentingViewController var bottomVC: UIViewController? while (parentVC != nil) { bottomVC = parentVC parentVC = parentVC?.presentingViewController } if bottomVC == nil { if let window = UIApplication.shared.delegate?.window { window?.rootViewController = nav } } else { bottomVC?.dismiss(animated: false, completion: { if let window = UIApplication.shared.delegate?.window { window?.rootViewController = nav } }) } } else { if let window = UIApplication.shared.delegate?.window { window?.rootViewController = nav } } } static func loginSuccess() { if let window = UIApplication.shared.delegate?.window { window?.rootViewController = TabBarController() } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
func getTopVC() -> (UIViewController?) { var window = UIApplication.shared.keyWindow if window?.windowLevel != UIWindow.Level.normal{ let windows = UIApplication.shared.windows for windowTemp in windows{ if windowTemp.windowLevel == UIWindow.Level.normal{ window = windowTemp break } } } let vc = window?.rootViewController return getTopVC(withCurrentVC: vc) }
func getTopVC(withCurrentVC vc :UIViewController?) -> UIViewController? { if vc == nil { print("🌶: 找不到顶层控制器") return nil } if let presentVC = vc?.presentedViewController { return getTopVC(withCurrentVC: presentVC) }else if let tabVC = vc as? UITabBarController { if let selectVC = tabVC.selectedViewController { return getTopVC(withCurrentVC: selectVC) } return nil } else if let naiVC = vc as? UINavigationController { return getTopVC(withCurrentVC:naiVC.visibleViewController) } else { return vc } }
|