extension AppDelegate {
    static var shared: AppDelegate {
        return UIApplication.shared.delegate as! AppDelegate
    }
    
    var key: UIWindow? {
        if #available(iOS 13, *) {
            return UIApplication.shared.windows.first { $0.isKeyWindow }
//            return UIApplication.shared.connectedScenes.compactMap{
//                $0 as? UIWindowScene
//            }.first?.windows.filter{
//                $0.isKeyWindow
//            }.first
        } else {
            return UIApplication.shared.keyWindow
        }
    }
}

extension UIApplication {
	class func currentTopViewController(baseVC: UIViewController? = AppDelegate.shared.key?.rootViewController) -> UIViewController? {
        
        if let navigationController = baseVC as? UINavigationController {
            return currentTopViewController(baseVC: navigationController.visibleViewController)
        }
        
        if let tabbarController = baseVC as? UITabBarController {
            if let selected = tabbarController.selectedViewController {
                return currentTopViewController(baseVC: selected)
            }
        }
        if let presented = baseVC?.presentedViewController {
            return currentTopViewController(baseVC: presented)
        }
        
        print("topViewController -> \(String(describing: baseVC))")
        return baseVC
        
    }
}
 

사용방법

let downloadVC: DownloadViewController = UIStoryboard(storyboard: .Main).instantiateViewController()
                
downloadVC.modalPresentationStyle = .overFullScreen
                
guard let topVC = UIApplication.currentTopViewController() else {
	return false
}
                
topVC.present(downloadVC, animated: false)

+ Recent posts