■
先日iOS5がリリースされましたが、キーボードの処理で変更があったため、対応したメモ。
iOS4(iPhone/iPod touch)まではキーボードの高さは216だった。
しかし、日本語キーボードの予測変換がキーボードの上に表示されるようになり、その際には高さが252と変更された。
キーボードの上にツールバーを設置していたため日本語キーボードに切り替えた場合、予測変換でツールバーが見えなくなってしまうという現象に陥ったため対応したのが経緯。
以下、キーボードの上に高さ40のツールバーを設置した時の処理
※2011/10/26 更新
iOS5でのキーボード処理について【追記】 - NoasMarkのプログラムMemo(まれに雑記)
/*! * @brief 共通処理 */ - (void)KeyboardCommonFunc:(NSNotification *)notification { CGRect keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; NSLog(@"keyboardRect.size.height = %f", keyboardRect.size.height); if (keyboardRect.size.height > 0.f) { // ツールバーの高さが40の場合. (画面サイズは固定だがサンプルなので320x480として扱う). [toolBar setFrame:CGRectMake(0, 480 - keyboardRect.size.height - 40, 320, 40)]; } } /*! * @brief キーボード表示通知 */ - (void)keyboardWillShowNotification:(NSNotification *)notification { NSLog(@"keyboardWillShowNotification call"); [self KeyboardCommonFunc:notification]; } /*! * @brief キーボード変更通知 */ - (void)keyboardWillChangeNotification:(NSNotification *)notification { NSLog(@"keyboardWillChangeNotification call."); [self KeyboardCommonFunc:notification]; } /*! * @brief View表示通知 */ - (void)viewWillAppear:(BOOL)animated { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil]; const float version = [[[UIDevice currentDevice] systemVersion] floatValue]; if (version >= 4.2f) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeNotification:) name:UITextInputCurrentInputModeDidChangeNotification object:nil]; } } /*! * @brief View非表示通知 */ - (void)viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; const float version = [[[UIDevice currentDevice] systemVersion] floatValue]; if (version >= 4.2f) { [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextInputCurrentInputModeDidChangeNotification object:nil]; } }
言語が変わった時はキーボードの高さ情報は0のようなので、サイズ判定をして0以上ならツールバーの位置を変更するようにしている。
言語が変わった場合にツールバーの情報を変えたりする場合はUITextInputCurrentInputModeDidChangeNotificationで指定したメソッドで処理をすれば問題ないかと。
// 1. // 表示(日本語キーボード). keyboardWillShowNotification call keyboardRect.size.height = 252.000000 keyboardWillShowNotification call keyboardRect.size.height = 252.000000
// 2. // 日本語→英語. keyboardWillChangeNotification call keyboardRect.size.height = 0.000000 keyboardWillShowNotification call keyboardRect.size.height = 216.000000
// 3. // 英語→日本語. keyboardWillChangeNotification call keyboardRect.size.height = 0.000000 keyboardWillShowNotification call keyboardRect.size.height = 252.000000