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

store cgpoint in userdefaults iOS swift