iosxcodenotificationsfirst-responder

Why is UIKeyboardWillShowNotification called every time another TextField is selected?


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


Solution

  • 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