How do I stop the keyboard from blocking an external notification's close buttons on iOS?
I have a UIAlertView with a text field in it. This view works correctly; it appears in the top half of the screen, so when the keyboard comes up, both buttons and the text entry are still visible and tappable.
However, if a local or remote notification from another application comes in while the text field is the first responder and the keyboard is up, it hides my alert and places the new alert behind the keyboard. If the alert is more than one line long, its buttons cannot be clicked and the user cannot dismiss it. It's not dismissing my alert view via any of the normal mechanisms - neither alertView:clickedButtonAtIndex:
or alertView:cancel:
get called on my delegate - but rather simply hides it temporarily using some unknown mechanism.
Can I get notified when a notification hides my alert view, so I can dismiss the keyboar开发者_如何转开发d and let the user deal with the notification? Are there any other events I can hook into to catch this case?
Register for the application’s UIApplicationWillResignActiveNotification
; when you get that, dismiss your alert view. From the docs:
An active application can be said to have focus. It gains focus after being launched, loses focus when an overlay window pops up or when the device is locked, and gains focus when the device is unlocked.
Alert views, such as those presented by notifications from other apps, are overlay windows. Watch for that notification, close your alert so the keyboard doesn’t get in the way, and be prepared to re-display the alert if your app then gets the UIApplicationDidBecomeActiveNotification
.
I hope this will help...
- (void)willPresentAlertView:(UIAlertView *)alertView {
if (newAlertViewWhichBlocks.tag == alertView.tag) {
[yourTextField resignFirstResponder];
}
}
Do remember to set delegate of alert view.
精彩评论