Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent the UIKeyboard from animating away and back after dismissing a UIAlertView
    primarykey
    data
    text
    <p>I'm wondering if any iOS devs here have ran into this issue and might be able to propose a solution. It involves the behavior of the UIKeyboard as it relates to a UITextView and UIAlertView.</p> <p>In an app, tapping a UITextView invokes the UIKeyboard. Tapping a button in the UITextView's accessory view invokes a UIAlertView with the UIAlertViewStyleLoginAndPasswordInput style. So far all is good.</p> <p>Here's the badness: dismissing the UIAlertView causes the UIKeyboard to animate away, and then back as the UITextView becomes the first responder again. The desired behavior is to skip the animation. Calling [textView becomeFirstResponder] in the UIAlertViewDelegate methods doesn't seem to do the trick.</p> <p>Here <a href="https://github.com/aerych/responder-test" rel="nofollow">is a sample project</a> that illustrates the behavior, and a relevant code snippet and log is posted below. </p> <p>Thoughts on this?</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; UIBarButtonItem *alertButton = [[UIBarButtonItem alloc] initWithTitle:@"Alert" style:UIBarButtonItemStyleBordered target:self action:@selector(handleAlertButtonTapped:)]; UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(handleDoneButtonTapped:)]; UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0)]; toolbar.barStyle = UIBarStyleBlack; toolbar.items = @[alertButton, spacer, doneButton]; UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0)]; [accessoryView addSubview:toolbar]; textView.inputAccessoryView = toolbar; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; } - (void)handleAlertButtonTapped:(id)sender { NSLog(@"%@", NSStringFromSelector(_cmd)); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Styled AlertView" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; [alertView show]; } - (void)handleDoneButtonTapped:(id)sender { [textView resignFirstResponder]; } #pragma mark - #pragma mark TextView Delegate Methods - (BOOL)textViewShouldEndEditing:(UITextView *)textView { NSLog(@"%@", NSStringFromSelector(_cmd)); return YES; } - (void)textViewDidEndEditing:(UITextView *)textView { NSLog(@"%@", NSStringFromSelector(_cmd)); } #pragma mark - #pragma mark AlertView Delegate methods - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"%@", NSStringFromSelector(_cmd)); [textView becomeFirstResponder]; // Not a solution. } - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex { NSLog(@"%@", NSStringFromSelector(_cmd)); [textView becomeFirstResponder]; // Not a solution. } - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { NSLog(@"%@", NSStringFromSelector(_cmd)); [textView becomeFirstResponder]; // Not a solution. } #pragma mark - #pragma mark Keyboard Notification Methods - (void)handleKeyboardWillShow:(NSNotification *)notification { NSLog(@"%@", NSStringFromSelector(_cmd)); [textView becomeFirstResponder]; // Not a solution. } - (void)handleKeyboardDidShow:(NSNotification *)notification { NSLog(@"%@", NSStringFromSelector(_cmd)); [textView becomeFirstResponder]; // Not a solution. } - (void)handleKeyboardWillHide:(NSNotification *)notification { NSLog(@"%@", NSStringFromSelector(_cmd)); [textView becomeFirstResponder]; // Not a solution. } - (void)handleKeyboardDidHide:(NSNotification *)notification { NSLog(@"%@", NSStringFromSelector(_cmd)); [textView becomeFirstResponder]; // Not a solution. } </code></pre> <p>And the result of the logs is something like this:</p> <pre><code>ResponderTest[1228:11303] handleKeyboardWillShow: ResponderTest[1228:11303] handleKeyboardDidShow: ResponderTest[1228:11303] handleAlertButtonTapped: ResponderTest[1228:11303] handleKeyboardWillShow: ResponderTest[1228:11303] handleKeyboardDidShow: ResponderTest[1228:11303] alertView:clickedButtonAtIndex: ResponderTest[1228:11303] alertView:willDismissWithButtonIndex: ResponderTest[1228:11303] handleKeyboardWillHide: ResponderTest[1228:11303] handleKeyboardDidHide: ResponderTest[1228:11303] handleKeyboardWillShow: ResponderTest[1228:11303] handleKeyboardDidShow: ResponderTest[1228:11303] handleKeyboardWillHide: ResponderTest[1228:11303] handleKeyboardDidHide: ResponderTest[1228:11303] handleKeyboardWillShow: ResponderTest[1228:11303] alertView:didDismissWithButtonIndex: ResponderTest[1228:11303] handleKeyboardDidShow: </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload