Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a second UIWindow to display the UIAlertController. #18

Merged
merged 2 commits into from
May 5, 2015

Conversation

dbettermann
Copy link

Problem:
With UIAlertView we could call show() from the AppDelegate and have the UIAlertView implementation find the visible view controller and add the view as a subview. This no longer works with the new iOS 8 UIAlertController because it's a subclass of UIViewController (instead of UIView) and has to be presented just like any other view controller. Calling presentViewController(...) on the UIWindow's rootViewController will not work if the rootViewController is not visible when the app becomes active. We need a way to display the UIAlertController without calculating (or passing a reference to) the visible view controller.

Solution:
By using a second UIWindow we can ensure that its rootViewController is always the visible view controller. If we set the windowLevel of the second UIWindow to be UIWindowLevelAlert + 1 we ensure that the window will always appear above any other views and view controllers in the app. Lastly, we need to create a strong reference to the second window via an associated object. If we do not do this, the window will be released before it's displayed.

if UIApplication.sharedApplication().updaterWindow == nil {
let window = UIWindow(frame: UIScreen.mainScreen().bounds)
window.rootViewController = UIViewController()
window.windowLevel = UIWindowLevelAlert + 1 // make the window appear above all other view controllers and windows
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This relies on undocumented behavior - I suggest we stick with UIWindowLevelAlert.

@ArtSabintsev
Copy link
Owner

I'll look at this in detail over the next few days and confer with @getaaron before accepting the PR.

*/
if UIApplication.sharedApplication().updaterWindow == nil {
let window = UIWindow(frame: UIScreen.mainScreen().bounds)
window.rootViewController = UIViewController()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that this fixes the rotation issue I thought would exist. Clever! Have you tested this code on iOS 7? Does it still work as expected?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly certain that it will work on iOS 7, but I'll test and confirm.

@getaaron
Copy link
Contributor

@dbettermann This looks excellent. I haven't tested it on iOS 7, but if it works there, it's certainly an improvement over what we have now. I left some minor suggestions, mostly stylistic.

Thanks for the PR.

@dbettermann
Copy link
Author

@getaaron @ArtSabintsev Thank you for considering it. I'll make some changes based on the feedback later today.

@dbettermann
Copy link
Author

Made some changes that align with the feedback. Check it out!

@ArtSabintsev
Copy link
Owner

Hah, this is pretty clever from the looks of it. Will DL and accept the PR tonight if everything works. Great job!

@ArtSabintsev
Copy link
Owner

Looks excellent. Accepting the PR and pushing a commit over it to give you credit! Great job my man!

Now, I need to make these changes in the ObjC repo... =p I'll give you credit there once I get to it.

@ArtSabintsev ArtSabintsev reopened this May 5, 2015
ArtSabintsev added a commit that referenced this pull request May 5, 2015
Adding a second UIWindow to display the UIAlertController.
@ArtSabintsev ArtSabintsev merged commit 8fc676b into ArtSabintsev:master May 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants