ykyouhei / KYDrawerController

Side Drawer Navigation Controller similar to Android
MIT License
621 stars 158 forks source link

Top most view controller ? #129

Open subhash08 opened 6 years ago

subhash08 commented 6 years ago

I'm using your DrawerController , thank you for awesome library. My issue:

I'm using KYDrawerController with UITabbarController, I need to get Top Most controller of the application which is showing now but I'm not able to get correct one.

here is the code I'm using.

extension UIViewController {
    func topMostViewController() -> UIViewController {
        if let navigation = self.presentedViewController as? UINavigationController {
            if let visibleController = navigation.visibleViewController {
                return visibleController.topMostViewController()
            }
        }
        if let tab = self.presentedViewController as? UITabBarController {
            if let selectedTab = tab.selectedViewController {
                return selectedTab.topMostViewController()
            }
            return tab.topMostViewController()
        }
        if self.presentedViewController == nil {
            return self
        }

        return self.presentedViewController!.topMostViewController()
    }
}

extension UIApplication {
    func topMostViewController() -> UIViewController? {
        return self.keyWindow?.rootViewController?.topMostViewController()
    }
}

Please correct me if I'm using wrong approch.