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)
'Swift > UIViewController' 카테고리의 다른 글
[SWIFT]UIDocumentPickerViewController (0) | 2023.06.07 |
---|---|
[SWIFT]뷰 회전 관련 작업 : viewWillTransition (0) | 2023.04.24 |
[SWIFT]IOS16 화면 회전상태 확인 (0) | 2023.04.18 |
[SWIFT]UIViewController SafeArea 영역 확인 (0) | 2023.04.17 |
[SWIFT]UIStoryboard 확장 UIViewController 쉽게 불러오기 (0) | 2023.04.17 |