Created
April 10, 2017 01:37
-
-
Save hongkheng/5a9c84806d58230bfe4720f9d92360e3 to your computer and use it in GitHub Desktop.
Find the rootViewController to present alert globally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if var topController = UIApplication.shared.keyWindow?.rootViewController { | |
while let presentedViewController = topController.presentedViewController { | |
topController = presentedViewController | |
} | |
// topController should now be your topmost view controller | |
// Dismissing an UIAlertController which is being displayed and setting the topController down one level | |
if topController is UIAlertController { | |
if let vc = topController.presentingViewController { | |
topController = vc | |
vc.dismiss(animated: true, completion: nil) | |
} | |
} | |
// Now present the alert controller | |
topController.present(self, animated: true, completion: nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment