20

I have a project that contains a UIScrollView and many UITextField inside it.

For the first time I select a UITextField, UIKeyboardWillShowNotification is called, which is fine. But whenever I select new UITextField (THE KEYBOARD IS STILL THERE), UIKeyboardWillShowNotification is called again !!!, which is weird.

I also set a symbolic breakpoint for [UIResponder resignFirstResponder] and I see that it is hit before and after UIKeyboardWillShowNotification is called !!!

The other thing is that UIKeyboardWillHideNotification is only called when I hit the "Done" button on the keyboard

I'm sure to not call any resignFirstResponder, becomeFirstResponder, endEditing anywhere. (I mean not call wrongly)

What can cause this problem ?

Here is the stacktrace enter image description here

12
  • Why do you see this as a problem?
    – matt
    Mar 21, 2014 at 4:11
  • @matt Just edited it, I mean the keyboard is still there, but why UIKeyboardWillShowNotification is called again ?
    – onmyway133
    Mar 21, 2014 at 4:19
  • By default, it should not be called twice so there must be something else going on in your code to provoke this action. If you create a project with two text fields and implement a notification using the UIKeyboardWillShowNotification name, your selector will only be called once.
    – Chris
    Mar 21, 2014 at 4:21
  • @Chris thanks, so what can cause this problem? Some Gesture Recognizers, .. ?
    – onmyway133
    Mar 21, 2014 at 4:24
  • Are you doing anything odd with gesture recognizers? Are you doing anything odd with becomeFirstResponder?
    – matt
    Mar 21, 2014 at 4:25

6 Answers 6

21

To workaround the problem, I used the following code to cancel the UIKeyboardWillShowNotification callback if the keyboard's frame is not changing.

func keyboardWillShow(notification: NSNotification) {

    let beginFrame = notification.userInfo![UIKeyboardFrameBeginUserInfoKey]!.CGRectValue()
    let endFrame = notification.userInfo![UIKeyboardFrameEndUserInfoKey]!.CGRectValue()

    // Return early if the keyboard's frame isn't changing.
    guard CGRectEqualToRect(beginFrame, endFrame) == false else {
        return
    }

    ...
}

For Swift 3/4:

func keyboardWillShow(notification: Notification) {

    let userInfo = notification.userInfo!
    let beginFrameValue = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)!
    let beginFrame = beginFrameValue.cgRectValue
    let endFrameValue = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)!
    let endFrame = endFrameValue.cgRectValue

    if beginFrame.equalTo(endFrame) {
        return
    }

    // Do something with 'will show' event
    ...
}
3
  • Much more elegant! Thanks for this!
    – IIllIIll
    Apr 30, 2017 at 20:01
  • I believe beginFrame.equalTo(endFrame) is the syntax for swift 3
    – SymbolixAU
    Oct 31, 2017 at 1:30
  • Thanks for the answer. This really helped me in my situation. Apr 8, 2020 at 20:22
16

The problem is I set inputAccessoryView for the UITextField, and this cause UIKeyboardWillShowNotification being called again when new UITextField is selected

This article Working With Keyboard on iOS explains this well

Additional changes take place when we connect an external keyboard to the iPad. In this particular case, the notification behavior depends on the inputAccessoryView property of the control which was the reason for displaying the keyboard.

If inputAccessoryView is not present or its height is equal to 0 points, no keyboard notifications are sent. My guess is that this is because in this case, no visual changes take place in application. Otherwise, all notifications behave as expected – which means they are being sent as in the majority of cases when the keyboard is displayed or hidden in a normal (not undocked or split) state.

Whenever new UITextField is selected, the OS needs to compute the frame for the keyboard again, and the following notifications are posted

UIKeyboardWillChangeFrameNotification
UIKeyboardWillShowNotification
UIKeyboardDidChangeFrameNotification
UIKeyboardDidShowNotification

The same applies for when the TextField loses its first responder status

Note that using the same View for inputAccessoryView will cause UIKeyboardWillShowNotification only called once

2
  • Can you please help me understand what is the solution to avoid receiving notification twice, I mean what is to be done in code ? Sep 24, 2014 at 8:16
  • Under iOS 9, I am reusing the same view for my new text view's inputAccesoryView and both UIKeyboardWillChangeFrameNotification and UIKeyboardWillShowNotification are fired each time the new text view becomes active. Under iOS 8, no notifications are generated under this scenario.
    – Evan R
    May 9, 2016 at 18:15
5

In general I find that many things can cause spurious UIKeyboardWillShow and UIKeyboardWillHide notifications. My solution is to use a property to track whether the keyboard is already showing:

func keyboardShow(_ n:Notification) {
    if self.keyboardShowing {
        return
    }
    self.keyboardShowing = true
    // ... other stuff
}

func keyboardHide(_ n:Notification) {
    if !self.keyboardShowing {
        return
    }
    self.keyboardShowing = false
    // ... other stuff
}

Those guards block exactly the spurious notifications, and all is well after that. And the keyboardShowing property can be useful for other reasons, so that it is something worth tracking anyway.

2
  • Nice answer. Very simple. However if the app needs to update its UI based on changes in keyboard height, this will guard out that attempt. For example, assume that the keyboard is open in a password textfield, and the phone receives the OTP code in SMS, the quick-type bar will be shown on the keyboard to autofill, changing its height. In that case, @paulvs' answer works really well. Apr 8, 2020 at 20:28
  • 3
    @badhanganesh Yes, I no longer use that approach. I use a much more sophisticated approach, seen here: github.com/mattneub/Programming-iOS-Book-Examples/blob/master/…
    – matt
    Apr 8, 2020 at 21:13
1

The best approach is to Add notification & remove it once your purpose is solve.

like this .

- (void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

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

Now write your code for movement of views & textField in keyboardWillShow & revert them back to position in keyboardWillHide methods.

Also remove the observers

- (void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillShowNotification
                                              object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillHideNotification
                                              object:nil];
}

You can also resign the responder when you press return key.

-(BOOL)textFieldShouldReturn:(UITextField *)textField {

    [_txtFieldEmail resignFirstResponder];
    [_txtFieldPassword resignFirstResponder];
    return YES;
}

That should solve your issue.

1
  • Sorry but this doesn't solve the problem , when we leave the view we remove the notification and thats a standard practice nothing to do with the above mentioned problem , its something to do with accessory view. Sep 30, 2014 at 13:20
1

For those not using inputAccessoryView but are still having problems, it may be due to using sensitive (password) fields. See this Stack Overflow post and answer: keyboardWillShow in IOS8 with UIKeyboardWillShowNotification

0

I have struggled with this, after half a day of searching around and experimenting I think this is the shortest and most reliable code. It is a mix of a number of answers most of which I forget where I found (parts of which are mentioned here).

My problem was a WKWebView which (when the user changed fields) would spawn a load of WillShow, WillHide, etc notifications. Plus I had problem with the external keyboard which still has the onscreen touch bar thing.

This solution uses the same animation code to "Open" and "Close" the keyboard, it will also cope with external keyboard being attached and custom keyboard views.

First register for the UIKeyboardWillChangeFrameNotification.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:)                                                 name:UIKeyboardWillChangeFrameNotification object:nil];

Then you simply need to map the changes to your view in however you do it (change a hight or bottom constraint constant).

- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    CGRect keyboardEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect convertedEnd = [self.view convertRect:keyboardEnd fromView:nil];

    // Convert the Keyboard Animation to an Option, note the << 16 in the option
    UIViewAnimationCurve keyAnimation = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];

    // Change the Height or Y Contraint to the new value.
    self.keyboardHeightConstraint.constant = self.view.bounds.size.height - convertedEnd.origin.y;
    [UIView animateWithDuration:[userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]
                          delay:0.0
                        options:keyAnimation << 16
                     animations:^{
                         [self.view layoutIfNeeded];
                     } completion:nil];

}

The Animation to Option conversion seems to work (I can only find examples of it being used and not how/why), however, I am not convinced it will stay that way so it may be wise to use a "stock" option. It seems that the Keyboard uses some none specified Animation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.