I tried the best answer here however I found a problem in it. If you have another text field on the same page, you click the text field, show the keyboard. You will notice the text view shrinks. However, if you click the text view now, you will notice the text view size shrinks again while it should not.
My solution to this problem is to maintain a property in view controller representing keyboard state(shown/hide). If the keyboard is current visible, the text view should not be shrunk. In case you are using keyboards of different sizes for different text inputs, you should also maintain the old keyboard size.
Be aware that this solution also didn't take different orientation into account, which may affect the way you calculate the size of text view.
@implementation MyViewController {
BOOL keyboardShown;
NSInteger keyboardHeight;
}
- (void)moveTextViewForKeyboard:(NSNotification*)aNotification up: (BOOL) up{
NSDictionary* userInfo = [aNotification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect newFrame = self.textView.frame;
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
NSInteger oldHeight = self->keyboardShown ? self->keyboardHeight : 0;
NSInteger newHeight = up ? keyboardFrame.size.height : 0;
NSInteger change = oldHeight - newHeight;
self->keyboardShown = up;
self->keyboardHeight = keyboardFrame.size.height;
newFrame.size.height += change;
self.textView.frame = newFrame;
[UIView commitAnimations];
}