I can't seem to get the top most UIViewController
without access to a UINavigationController
. Here is what I have so far:
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(vc, animated: true, completion: nil)
However, it does not seem to do anything. The keyWindow
and rootViewController
seem to be non-nil values too, so the optional chaining shouldn't be an issue.
NOTE: It is a bad idea to do something like this. It breaks the MVC pattern.
Best Answer
presentViewController
shows a view controller. It doesn't return a view controller. If you're not using aUINavigationController
, you're probably looking forpresentedViewController
and you'll need to start at the root and iterate down through the presented views.For Swift 3+:
For iOS 13+