I'm trying to implement a UIAlertController
that is presented on top of the window and stays that way until the user closes it, even if the application attempts to push another view controller.
I've read those questions and answers:
ios - present UIAlertController on top of everything regardless of the view hierarchy
Present ViewController on top of everything regardless of the view hierarchy in IOS 13
And while in versions previous to iOS 13 the provided solutions worked like a charm, I'm stuck trying to solve this issue in iOS 13:
When I show the UIAlertController
, it stays in top until another view controller gets pushed in the navigation stack. If this happens, the UIAlertController
disappears.
func showHighPriorityNotification() {
let alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow.rootViewController = UIViewController()
alertWindow.windowLevel = UIWindowLevelAlert + 1
alertWindow.makeKeyAndVisible()
let alertController = UIAlertController(title: "Title"), message: "Message"), preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Button"), style: .default))
if #available(iOS 13, *) {
// Trying to get this functionality to work in iOS 13.
if var topController = alertWindow.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
topController.modalPresentationStyle = .fullScreen
topController.present(alertController, animated: true)
}
} else {
// This code works in iOS 12 and below, but NOT in iOS 13.
alertWindow.rootViewController?.present(alertController, animated: true)
}
}
In iOS13, is there some way to keep the UIAlertController
on top and allow views to get pushed below it? As I've said, in previous versions this just works nicely.