Skip to content

Instantly share code, notes, and snippets.

@hongkheng
Created April 10, 2017 01:37
Show Gist options
  • Save hongkheng/5a9c84806d58230bfe4720f9d92360e3 to your computer and use it in GitHub Desktop.
Save hongkheng/5a9c84806d58230bfe4720f9d92360e3 to your computer and use it in GitHub Desktop.
Find the rootViewController to present alert globally
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