Search code examples
iosuiscrollviewuitextfieldiphone-softkeyboard

TextField is not pushed up smoothly by keyboard, like in Whatsapp and Hangouts


I'm trying to mimic the UITextField-keyboard relation in Whatsapp app. As most of you know, there is a fixed UITextField at the bottom of the screen and keyboard comes up as you tap the UITextField. While keyboard is coming up, UITextField stick to the keyboard and they slide together very smoothly.

Read Apple's documentation pages which is about moving views under the keyboard several times.

What I have tried so far:

  • Listened to keyboard notifications. I used both setFrame: and setContentOffset:animated: method within animation block. Didn't work as good as expected. The UIViewAnimationCurve value that keyboard animation has is 7, I couldn't find what it means as UIViewAnimationCurve has only 4 integer enum type. I think animation curve of keyboard animation is not provided to developers, I hope I'm wrong.

  • Using custom inputAccessoryView with lower z-index value. I had two exact UIViews, one is fixed at the bottom and the other is custom inputAccessoryView that was going to hide beneath the fixed one since it has lower zPosition value. Therefore, user wasn't going to see the inputAccessoryView was coming up from outside of the screen but fixed view was being pushed up by keyboard. But no use, again. Changing zPosition value does not change anything.

At last, I thought Whatsapp might not be using system keyboard but a custom one. But if this is the case, it is a very very bad choice of design so that option is eliminated.

Anyone has an idea of how Whatsapp (or Hangouts) implemented that?


Solution

  • The most straightforward way of doing that turned out to be the correct way.

    I should have listened to keyboardWillShow/keyboardWillHide notifications and animateWithDuration: as they are triggered. The point I missed was casting UIViewAnimationCurve into UIViewAnimationOptionCurve by shifting (<<16).

    NSDictionary *notification = [aNotification userInfo];
    UIViewAnimationOptions curveValue = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue] << 16;
    ...
    [UIView animateWithDuration:{animation duration}
                          delay:0
                        options:curveValue
                     animations:^{
                         //animation code
                     }
                     completion:nil];