UIAlertView show() behavior for UIAlertController

前端 未结 4 1794
青春惊慌失措
青春惊慌失措 2020-12-17 04:55

In previous versions of iOS I was able to call show on a UIAlertView in the App Delegate. More specifically, show was called in:

fu         


        
4条回答
  •  轮回少年
    2020-12-17 05:02

    I figured out a solution that I believe to be more elegant than the answer I posted previously. I'll copy and paste the answer I posted to a similar question. Follow the link at the bottom of my post if you just want to see the code.

    The solution is to use an additional UIWindow.

    When you want to display your UIAlertController:

    1. Make your window the key and visible window (window.makeKeyAndVisible())
    2. Just use a plain UIViewController instance as the rootViewController of the new window. (window.rootViewController = UIViewController())
    3. Present your UIAlertController on your window's rootViewController

    A couple things to note:

    • Your UIWindow must be strongly referenced. If it's not strongly referenced it will never appear (because it is released). I recommend using a property, but I've also had success with an associated object.
    • To ensure that the window appears above everything else (including system UIAlertControllers), I set the windowLevel. (window.windowLevel = UIWindowLevelAlert + 1)

    Lastly, I have a completed implementation if you just want to look at that.

    https://github.com/dbettermann/DBAlertController

提交回复
热议问题