※本記事は、旧ブログからの再掲です。
画面の下の方のテキストフィールドに入力する際に、キーボードがテキストフィールドの上に表示されてしまうことがあります。
その場合にはキーボードが表示されるのに合わせて、入力しようとしているテキストフィールドを隠れないように上に引っ張り上げます。
※今回はスクロールビューを使用している場合の方法です。
contentOffsetのサイズをキーボードの高さ分追加し、編集中のテキストフィールドを一番上に持ってきています。
キーボードの表示・非表示時に通知を受けるためには、事前にその登録が必要となります。
通知の登録/解除は viewWillAppear/viewWillDisappear などの処理の中で行います。
UIKeyboardWillShowNotification(キーボード表示前に通知)
UIKeyboardWillHideNotification(キーボード非表示前に通知)
同時に通知を受けとるメソッドも登録します。
keyboardWillShow:(キーボード表示前に呼ばれるメソッド)
keyboardWillHide:(キーボード非表示前に呼ばれるメソッド)
keyboardWillShow:メソッドでは、キーボードの表示のアニメーションに合わせてスクロールビューのcontentSizeとcontentOffsetを変更し、テキストフィールドを上に引っ張り上げています。
keyboardWillHide:メソッドでは、変更したcontentSizeを元に戻しています。
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64  | 
						- (void)viewWillAppear:(BOOL)animated {     [super viewWillAppear:animated];     // キーボード表示・非表示の通知の登録     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } - (void)viewWillDisappear:(BOOL)animated {     [super viewWillDisappear:animated];     // キーボード表示・非表示の通知の解除     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } - (void)keyboardWillShow:(NSNotification *)notification {     // キーボードのCGRectを取得     NSDictionary *userInfo = [notification userInfo];     CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];     // 編集中テキストフィールドのRectを取得     CGRect editTextFieldRect = CGRectZero;     editTextFieldRect.origin.y = _textField.origin.y;     // contentSizeをキーボードの高さ分追加する     CGSize  newContentSize = CGSizeZero;     newContentSize = CGSizeMake(_scrollView.contentSize.width, _scrollView.contentSize.height+keyboardRect.size.height);     // contentOffsetを編集中テキストフィールドの座標に設定     CGPoint newOffset = CGPointZero;     newOffset = CGPointMake(0, editTextFieldRect.origin.y);     // キーボードのanimationDurationを取得     NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];     UIViewAnimationCurve animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];     // キーボードアニメーションと同じ間隔でテキストフィールドの位置をアニメーションしつつ変更する     [UIView animateWithDuration:duration delay:0.0  options:(animationCurve << 16) animations:^{ _scrollView.contentSize = newContentSize; _scrollView.contentOffset = newOffset; } completion:nil]; } - (void)keyboardWillHide:(NSNotification *)notification {        // キーボードのCGRectを取得     NSDictionary *userInfo = [notification userInfo];     CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];     // contentSizeを元に戻す     newContentSize = CGSizeMake(_scrollView.contentSize.width, _scrollView.contentSize.height-keyboardRect.size.height);     // キーボードのanimationDurationを取得     NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];     UIViewAnimationCurve animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];     // キーボードアニメーションと同じ間隔でcontentSizeを元に戻す     [UIView animateWithDuration:duration delay:0.0 options:(animationCurve << 16) animations:^{ _scrollView.contentSize = newContentSize; } completion:nil]; }  | 
					
<おすすめの書籍>
私がUIKitを使用する時に一番お世話になった本です。とても分かりやすいので初心者の方にもオススメです。