この現象は、iOS4以前でビルドされたアプリをiOS5環境のデバイスで実行した場合でも発生する。
この場合、キーボードが表示されたこと、あるいは入力モードが変更されたこと(日本語→英字、絵文字など)を感知してViewを固定する処理を記述しておけばよい。但し、デバイスのOSがiOS5以降でないとアプリが落ちるため、バージョンの判定を行い、iOS5.0以降の場合のみ、UITextInputCurrentInputModeDidChangeNotification の通知を行うこと。
- (void)viewDidLoad
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *versions = [[UIDevice currentDevice]systemVersion];
if ([versions compare:@"5.0"] != NSOrderedAscending ) { // iOS5.0以降の判定
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(keyboardWillChange:)
name:UITextInputCurrentInputModeDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(keyboardFrameWillChange:)
name:UIKeyboardWillChangeFrameNotification object:nil];
viewFrame = self.view.frame;
}
[pool release];
}
- (void)keyboardWillChange:(NSNotification*)notification{
self.view.frame = CGRectMake(0, 0, 320, 480); // iPhoneの場合
}
- (void)keyboardFrameWillChange:(NSNotification*)notification{
self.view.frame = CGRectMake(0, 0, 320, 480); // iPhoneの場合
}
self.view.frame = CGRectMake(0, 0, 320, 480);はiPhoneの場合。
iPadなど画面サイズのiOSデバイスでも対応できるようにするためには、viewDidLoadメソッドで
self.view.frameの内容を例えばviewFrameに保持し、
self.view.frame = CGRectMake(0, 0, viewFrame.size.width, viewFrame.size.height);のようにViewの横幅、高さを回復させる。
キーボードのフレームが変更されたときは、UIKeyboardWillChangeFrameNotification で検知する。
キーボードの表示されたとき、あるいは入力モードが変更されたときは、UITextInputCurrentInputModeDidChangeNotification で検知する。
参考記事:
http://bit.ly/oyxMc3
http://ai-lab.biz/blog/development/entry-320.html
【キーボードの最新記事】