UIKeyboardWillShowNotification, UIKeyboardWillHideNotification and NSNotificationCenter problem between iOS versions

15,533

Solution 1

What you can do is set the textfield's/textview's delegate to the current view controller and implement these 2 methods

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    _keyboardWillHide = NO;
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    _keyboardWillHide = NO;
    return YES;    
}

After that in your method that get's triggered by the UIKeyboardWillHideNotification notification you can do something like

if (_keyboardWillHide) {
    // No other textfield/textview was selected so you can animate the tableView
    ...
}
_keyBoardWillHide = YES;

Let me know if that works for you.

Solution 2

Rather than avoid the notifications, you can set an NSTimer for 0.1 second to do your animations in one, and in the other, cancel the timer, that way if you get UIKeyboardWillHide and UIKeyboardWillShow both at once, you'll get a chance to cancel the timer. If you don't get both, the timer will reach zero and the animations will be carried out.

Solution 3

Consider using the UITextFieldDelegate protocol. The method textFieldShouldBeginEditing: will fire off before the notification and it will fire off everytime you go into the text field.

Share:
15,533

Related videos on Youtube

CristiC
Author by

CristiC

Self-teaching is the most powerfull instrument of all.

Updated on June 04, 2022

Comments

  • CristiC
    CristiC over 1 year

    I have several UITextFields on my view (each inside a UITableViewCell). When the keyboard is fired from any of the textfields, I need to make some animations, mainly to change the frame of the UITableView. The same must happen when the keyboard will hide.

    I have done the animation, so this is not the issue here.

    Now, I use NSNotificationCenter to catch displaying/hiding of the keyboard:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
    

    The problem is when the keyboard is visible (a textfield is used) and I press inside another textfield. Usually for this thing keyboard will not hide, but will stay visible.

    It works fine in iOS 4, but the problem comes in 3.1.3 (this is the version that I can test - possibly any version below 3.2). In versions older than 3.2 changing focus from a textfield directly to another textfield will fire the UIKeyboardWillHideNotification and UIKeyboardWillShowNotification.

    Anyone knows a way to perform some animation when the keyboard will really show/hide, without the NSNotificationCenter?

    Or how can I overcome this issue with versions lower than 3.2?

    Thanks.

    • Peter DeWeese
      Peter DeWeese over 12 years
      A consideration: Almost everyone has upgraded to iOS 4 and those who haven't rarely download or update apps. One alternative is to switch to only supporting iOS 4 for new versions. Only bother with supporting both if your app is something on par of importance with a banking app or if you have some very special need to do so.
  • CristiC
    CristiC over 12 years
    This is a good idea. But cannot be done since this will create a small gap (delay) between the animation of the UIKeyboard and my own animation.
  • Alex Gosselin
    Alex Gosselin over 12 years
    Do the notifications always fire in a predictable order?
  • Alex Gosselin
    Alex Gosselin over 12 years
    You could consider starting the animations in the WillHide method, then in the WillShow method check if the animation is started, and interrupt it. That way if there isn't a show, they'll be carried out, otherwise they'll get stopped before anything happens. Assuming both methods get called in rapid fire that is
  • CristiC
    CristiC over 12 years
    Thanks, I will try it tonight and get back to you.
  • CristiC
    CristiC over 12 years
    It worked, but with small changes: In your answer _keyBoardWillHide = YES; was in UIKeyboardWillHideNotification. I moved it everywhere I had [UITextField resignFirstResponder];
  • devios1
    devios1 over 8 years
    @shim One is for UITextField, the other for UITextView.