Get the top of my app viewcontroller in iOS swift

 Problem:

I want to get the top viewcontroller of my project in current.How to get that ?


Solution:

extension UIApplication {

    class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {

        if let navigationController = controller as? UINavigationController {

            return topViewController(controller: navigationController.visibleViewController)

        }

        if let tabController = controller as? UITabBarController {

            if let selected = tabController.selectedViewController {

                return topViewController(controller: selected)

            }

        }

        if let presented = controller?.presentedViewController {

            return topViewController(controller: presented)

        }

        return controller

    }

}

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift