currentTopViewContoller 는 어느 위치에서든 현재 최상위 VC를 호출해서 present 할 수 있다.

import UIKit

extension UIApplication {
    
    class func keyWindow() -> UIWindow? {
        if #available(iOS 13, *) {
            return UIApplication.shared.connectedScenes
                //SwiftUI 에서 권한설정 UI등이 노출될때 nil일경우 있어서 주석처리함
                //.filter {$0.activationState == .foregroundActive}
                .compactMap {$0 as? UIWindowScene}
                .first?.windows.filter {$0.isKeyWindow}.first
        } else {
            return UIApplication.shared.keyWindow
        }
    }
    
    // MARK: TopViewController
    class func currentTopViewController() -> UIViewController? {
        guard let rootController = keyWindow()?.rootViewController else {
            return nil
        }
        return topMostViewController(for: rootController)
    }
    
    class func topMostViewController(for controller: UIViewController) -> UIViewController {
        if let presentedController = controller.presentedViewController {
            return topMostViewController(for: presentedController)
        } else if let navigationController = controller as? UINavigationController {
            guard let topController = navigationController.topViewController else {
                return navigationController
            }
            return topMostViewController(for: topController)
        } else if let tabController = controller as? UITabBarController {
            guard let topController = tabController.selectedViewController else {
                return tabController
            }
            return topMostViewController(for: topController)
        }
        return controller
    }
    
    class func getsafeAreaTopMargin() -> CGFloat {
        if #available(iOS 11.0, *) {
            let currentwindow = keyWindow()//UIApplication.shared.windows.first
            return currentwindow?.safeAreaInsets.top ?? 0.0
        }
        else {
            return 0
        }
    }
    
    class func getsafeAreaBottomMargin() -> CGFloat {
        if #available(iOS 11.0, *) {
            let currentwindow = keyWindow()//UIApplication.shared.windows.first
            return (currentwindow?.safeAreaLayoutGuide.owningView?.frame.size.height)! - (currentwindow?.safeAreaLayoutGuide.layoutFrame.size.height)! - (currentwindow?.safeAreaLayoutGuide.layoutFrame.origin.y)!
        }
        else {
            return 0
        }
    }
    
    class func getsafeAreaLeftMagin() -> CGFloat {
        if #available(iOS 11.0, *) {
            let currentwindow = keyWindow()//UIApplication.shared.windows.first
            return currentwindow?.safeAreaInsets.left ?? 0.0
        }
        else {
            return 0
        }
    }
    
    class func getsafeAreaRightMagin() -> CGFloat {
        if #available(iOS 11.0, *) {
            let currentwindow = keyWindow()//UIApplication.shared.windows.first
            return currentwindow?.safeAreaInsets.right ?? 0.0
        }
        else {
            return 0
        }
    }
}

 

사용방법

let webViewVC: BaseWebViewController = UIStoryboard(storyboard: .Common).instantiateViewController()

webViewVC.modalPresentationStyle = .overFullScreen
webViewVC.modalTransitionStyle = UIModalTransitionStyle.crossDissolve

guard let topVC = UIApplication.currentTopViewController() else {
     return
}
topVC.present(webViewVC, animated: true) {
}

+ Recent posts