Skip to content

Instantly share code, notes, and snippets.

@AnselmeKotchap
Last active August 10, 2016 22:11
Show Gist options
  • Save AnselmeKotchap/e3d1ab30195d7718c8623ab5443c46a3 to your computer and use it in GitHub Desktop.
Save AnselmeKotchap/e3d1ab30195d7718c8623ab5443c46a3 to your computer and use it in GitHub Desktop.
// so it won't compile on a lower version.
extension UIAlertController {
func show() {
present(animated: true, completion: nil)
}
func present(animated animated: Bool, completion: (() -> Void)?) {
if let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController {
presentFromController(rootVC, animated: animated, completion: completion)
}
}
private func presentFromController(controller: UIViewController, animated: Bool, completion: (() -> Void)?) {
if let navVC = controller as? UINavigationController,
let visibleVC = navVC.visibleViewController {
presentFromController(visibleVC, animated: animated, completion: completion)
} else {
if let tabVC = controller as? UITabBarController,
let selectedVC = tabVC.selectedViewController {
presentFromController(selectedVC, animated: animated, completion: completion)
} else { if let presentedViewController = controller.presentedViewController { presentedViewController.presentViewController(self, animated: animated, completion: completion)
} else {
controller.presentViewController(self, animated: animated, completion: completion)
}
}
}
}
}
//Present alert on always on top of any other view . Alternate approach
let alertWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindowLevelAlert + 1;
alertWindow.makeKeyAndVisible()
alertWindow.rootViewController?.presentViewController(alert, animated: true, completion: nil)
//source http://stackoverflow.com/questions/26554894/how-to-present-uialertcontroller-when-not-in-a-view-controller/36540728#36540728
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment