Copyright (C) iPhoneアプリ開発備忘録 All rights reserved.
ブログ内で記したコード、内容の正確性は保証いたしません。
記載内容を実装したことにより発生した不具合・損害等の責任は一切負いません。



2013年01月13日

キーボードが表示されたときにText Viewのサイズを調整する



キーボードが表示されたとき、キーボードは画面上部にスライドして、 その下のコンテンツを隠す。
従って、Text Viewを使ってテキストを入力させているときは、全領域を見えるようにするためにText Viewの枠を調整する必要がある。

1. キーボード表示の通知を登録する
- (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];
}


2. キーボード通知の登録を解除する
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

// 通知の受け取りを登録解除する
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
}


3. キーボードを表示するときに、Text Viewの枠を調整する
- (void)keyboardWillShow:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGRect kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]
CGRectValue];
double duration = [[info
objectForKey:UIKeyboardAnimationDurationUserInfoKey]
doubleValue];
UIEdgeInsets insets = self.textView.contentInset;
insets.bottom += kbSize.size.height;
[UIView animateWithDuration:duration animations:^{
self.textView.contentInset = insets;
}];
}


4. キーボードを閉じるときに、Text Viewの枠を元に戻す
- (void)keyboardWillHide:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
double duration = [[info
objectForKey:UIKeyboardAnimationDurationUserInfoKey]
doubleValue];
// Text Viewの下部コンテンツ挿入枠をリセットする
UIEdgeInsets insets = self.textView.contentInset;
insets.bottom = 0;
[UIView animateWithDuration:duration animations:^{
self.textView.contentInset = insets;
}];

}



参考記事:
https://developer.apple.com/jp/devcenter/ios/library/documentation/iCloud101.pdf



posted by mobileDeveloper at 23:06 | Comment(0) | TrackBack(0) | キーボード はてなブックマーク - キーボードが表示されたときにText Viewのサイズを調整する | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。

この記事へのトラックバック
Apple、Appleのロゴ、App Store、iPodのロゴ、iTunesは、米国および他国のApple Inc.の登録商標です。
iPhone、iPod touch、iPadはApple Inc.の商標です。
iPhone商標は、アイホン株式会社のライセンスに基づき使用されています。
その他、本ブログに記載されている製品名、会社名は、それぞれ各社の商標または登録商標です。