I was developing an iPhone application screen using the techniques described in my previous post How To Create A Data Entry Screen and I had the additional requirement to hide the keyboard after data entry was complete.
In addition to UIKeyboardWillShowNotification:s I was also getting UIKeyboardWillHideNotification:s so that I would know when the OS hides the keyboard. To my surprise that turns out to be very frequent: each time you move between fields. As each control resigns as first responder an UIKeyboardWillHideNotification is sent, immediately followed by a UIKeyboardWillShowNotification.
The iPhone is smart enough to not actually move the keyboard in and off the display (or it’s done so quickly that it’s imperceptible.) However in the keyboard notification methods I was adjusting the size of the scroll view to match the available screen real estate. And this led to some ugly flickering.
So I created an instance variable: BOOL isEnteringData; to keep track of when the user is actively entering data. When any control on the screen becomes first responder (textFieldDidBeginEditing) then set isEnteringData to YES. When Return or Done is pressed (textFieldShouldReturn) in the last control, then set isEnteringData to NO.
In a keyboardWillHide method I now first check isEnteringData before resizing the scroll view. And flicker be gone.