28
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

キーボードにあわせてテキストビューの位置を変える(変換候補、予測変換対応バージョン)

Posted at

ググるといろいろネットで出てきたけど、情報が古かったりしていまいちわかりにくかったので、改めてここに投稿します。

//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のような見た目のアプリを作りたい人向けです。

28
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?