0

I am trying to show a UIAlertController over a UISplitViewController. I have tried everything that I have found on this site, and am sticking with an extension found here.

extension UIAlertController {

func presentInOwnWindow(animated: Bool, completion: (() -> Void)?) {
        let alertWindow = UIWindow(frame: UIScreen.main.bounds)
        alertWindow.rootViewController = UIViewController()
        alertWindow.windowLevel = UIWindowLevelAlert + 1;
        alertWindow.makeKeyAndVisible()
        alertWindow.rootViewController?.present(self, animated: animated, completion: completion)
}

I get an exception immediately after present executes, and I can't see the stack trace and the exception breakpoint is not fired.

libc++abi.dylib: terminating with uncaught exception of type NSException

It presents fine on a phone with the UISplitViewController collapsed. What am I missing?

Siriss
  • 3,737
  • 4
  • 32
  • 65
  • Are you calling this function on the main thread? Perhaps you should surround this code with DispatchQueue.main.async { ... } – adamfowlerphoto Nov 10 '17 at 19:05
  • Also you are setting rootViewController to a newly created UIViewController(). Shouldn't you be setting this to the current view controller – adamfowlerphoto Nov 10 '17 at 19:16
  • It is on the main thread. I actually removed a dispatch to main to post this because it was on the main thread anyway. The rootViewController is created and added as a root to the new window that overlays the currentViewController. – Siriss Nov 10 '17 at 19:55

1 Answers1

0

Potential problem is UIWindowLevelAlert + 1;. Try to use UIWindowLevelNormal instead. The new window will be placed above your default window. And above UISplitViewController too.

func presentInOwnWindow(animated: Bool, completion: (() -> Void)?) {
    let alertWindow = UIWindow(frame: UIScreen.main.bounds)
    alertWindow.rootViewController = UIViewController()
    alertWindow.windowLevel = UIWindowLevelNormal;
    alertWindow.makeKeyAndVisible()
    alertWindow.rootViewController?.present(self, animated: animated, completion: completion)
}
Alex
  • 1,155
  • 1
  • 8
  • 12
  • Still crashes. I have also tried removing windowLevel entirely. – Siriss Nov 10 '17 at 19:59
  • This works for me. Please review https://github.com/AlexeyGolovenkov/TestWindow. Try to add All Exceptions breakpoint (Breakpoint navigator -> + -> Exception breakpoint) and check the result. – Alex Nov 10 '17 at 20:21
  • I keep getting a crash. I am on 11.1. Also, you project has the master collapsed. It crashes with the master open, at least for me. – Siriss Nov 10 '17 at 22:23
  • It's strange. I don't see crash with any state of master controller. BTW I see alert even without custom window. Are you sure the problem is in alert? – Alex Nov 11 '17 at 07:49