[objc/swift] 최상위 최상단 레이어 ViewController 가져오기

천현철·2021년 1월 21일
0

iOS

목록 보기
20/51
post-custom-banner

Objective-c

+ (UIViewController*) topMostController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }

    return topController;
}

Swift 3.0+

func topMostController() -> UIViewController? {
    guard let window = UIApplication.shared.keyWindow, let rootViewController = window.rootViewController else {
        return nil
    }

    var topController = rootViewController

    while let newTopController = topController.presentedViewController {
        topController = newTopController
    }

    return topController
}
profile
기도하지말고 행동하라
post-custom-banner

0개의 댓글