iOS Dev Nugget 104 Animating Changes to View Height When Keyboard Appears/Disappears


Need to run a code review on your codebase? Hire me

In #20 Do not hardcode keyboard height, we talked about not hardcoding the keyboard height when we resize views in response to keyboard showing or hiding. This is especially important since we have keyboards of varying heights for a long time now. However, we hardcoded the animation parameters in the sample code. This is fragile and can be broken especially across iOS upgrades. However, this can be improved easily.

Just read the values for the animation duration and animation curve from the userInfo property of the notification with the keys UIKeyboardAnimationDurationUserInfoKey and UIKeyboardAnimationCurveUserInfoKey respectively and use them in the animation calls. Remember to do this both when the keyword is appearing and disappearing.

So you would have something like:

[UIView animateWithDuration:aNotification.userInfo[UIKeyboardAnimationDurationUserInfoKey] delay:0 options:aNotification.userInfo[UIKeyboardAnimationCurveUserInfoKey] animations:^{
    //Make your changes here
} completion:nil];

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No


Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter


View archives of past issues