ググるといろいろネットで出てきたけど、情報が古かったりしていまいちわかりにくかったので、改めてここに投稿します。
//viewDidAppear:(BOOL)animated等でこれを呼び出す
- (void)registKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];
}
//viewDidDisappear:(BOOL)animated等でこれを呼び出す
- (void)removeKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextInputCurrentInputModeDidChangeNotification object:nil];
}
//以下のメソッドを追加
- (void)keyboardWillShow:(NSNotification*)notification
{
CGRect keyboardFrameEnd = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
[self moveTextView:(self.navigationController.view.frame.size.height - textView.frame.size.height - keyboardFrameEnd.size.height) notification:notification];(*)
}
- (void)keyboardWillHide:(NSNotification*)notification
{
[self moveTextView:(self.navigationController.view.frame.size.height - textView.frame.size.height) notification:notification];(*)
}
- (void)moveTextView:(CGFloat)moveTo notification:(NSNotification*)notification
{
NSTimeInterval duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [[notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
void (^animations)(void);
animations = ^(void) {
CGRect keyboardFrameEnd = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
if (keyboardFrameEnd.size.height > 0.0f) {
textView.frame = CGRectMake(textView.frame.origin.x, moveTo, textView.frame.size.width, textView.frame.size.height);
}
};
[UIView animateWithDuration:duration delay:0.0f options:(curve << 16) animations:animations completion:nil];
}
私の場合は、NavigationControllerにテキストビューをのせているので上記のようになっていますが、それぞれの必要に応じて、(*)の部分は調整してください。
標準のメッセージアプリやLineのような見た目のアプリを作りたい人向けです。