When the alert pops up the keyboard is dismissed. I have looked everywhere but did not find solutions to keep the keyboard visible. When alert is presented the textfield seems to resign first responder automatically as the alert is presented modally. How is it possible to keep the keyboard behind this alert which means the textfield still editing even if no interaction will be possible ?
Asked
Active
Viewed 6,426 times
2 Answers
27
This solution works for me:
let rootViewController: UIViewController =
UIApplication.sharedApplication().windows.lastObject.rootViewController!!
rootViewController.presentViewController(alert, animated: true, completion: nil)
edit by @galambalazs: The reason it works is because:
You can grab the window with the current highest window level and present your View Controller inside that Window (making it the top View Controller in the top Window).
UIApplication.sharedApplication().windows
The windows in the array are ordered from back to front by window level;
thus, the last window in the array is on top of all other app windows.
Also you might want to set the tintColor of that window so that it matches your apps global tintColor.
UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject;
// we inherit the main window's tintColor because topWindow may not have the same
topWindow.tintColor = [UIApplication sharedApplication].delegate.window.tintColor;
-
1It looks like this is using the window the status bar is in to present the alert controller, right? – ninjudd Jun 02 '15 at 21:58
-
1Actually, I was wrong. The second window in `UIAppplication.sharedApplication().windows` is a UITextEffectsWindow. – ninjudd Jun 02 '15 at 22:17
-
1After more research, I discovered that UITextEffectsWindow is where the keyboard view (or UIViewController inputAccessoryView) is located. So this second window will only exist if the keyboard has been displayed. http://www.fantageek.com/1317/uiwindow-in-ios/ – ninjudd Jun 03 '15 at 01:09
-
1So that worked for me. Although the background turns black and that is not good if you are using an effect as background (using the actual view controller's view). Also the transition is not good (because replaces the actual view with a black background). Still looking for better solution... – Tzegenos Sep 08 '15 at 10:10
-
2I think you should use UIApplication.sharedApplication().windows.lastObject.rootViewController, then the code will work whether keyboard show or hide. – smoothdvd Sep 18 '15 at 05:34
-
1Is there another way other than accessing the internal window hierarchy? – NYC Tech Engineer Dec 30 '15 at 18:33
-
1Thanks. The solution only worked well in iOS 8, it does not work on iOS 9 though. Also Note in the answer, there is an extra `!` at the end, which is not needed. with Swift 2.1 – Zhao Feb 08 '16 at 21:01
-
1@smoothdvd 's solution works well for iOS 9 and 8. Although in iOS 8, I got some crashes sometimes. Looking into the issue. – Zhao Feb 08 '16 at 21:52
-
should `lastObject` be replaced by `last`? – He Yifei 何一非 Jun 20 '16 at 07:32
-
I am facing same problem when i do view.isUserInteractionEnabled = false , and true Any advice about it. – Yılmaz edis Aug 31 '23 at 08:45
3
For Swift 3 and iOS11
if let alertWindow = UIApplication.shared.windows.last, alertWindow.windowLevel == 10000001.0 // If keyboard is open
{ // Make sure keyboard is open
alertWindow.rootViewController?.present(alertController, animated: true, completion: nil)
}
else
{
viewController?.present(alertController, animated: true, completion: nil)
}

Hiren Panchal
- 2,963
- 1
- 25
- 21