これはかなり一般的なトピックであり、そこには多くの答えがあります。
状況:下部にUIToolbarを備えたフルスクリーン(ツールバーを除く)UITextViewがあります。UITextViewがファーストレスポンダーを取得したら、ツールバーをキーボードと一緒に上にスライドさせ、キーボードを閉じる「完了」ボタンを追加します。
これまでのところ:この例に基づいて、これは完全に機能しています。[textView becomeFirstResponder];
私がviewDidLoad
それを入れたとき、ツールバーがアニメートしないという事実を除いて。keyboardWillShow
と呼ばれていても。誰かが何か考えを持っていますか?
コード:サンプルコードをチェックする必要がないように、これが起こっていることです:
viewDidLoadの場合:
- (void)viewDidLoad {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[textView becomeFirstResponder];
[super viewDidLoad];
}
KeyboardWillShowの場合:
- (void)keyboardWillShow:(NSNotification *)notification {
NSLog(@"keyboard will show");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(keyboardDone)];
NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:[toolbar items]];
[toolbarItems addObject:doneButton];
[toolbar setItems:toolbarItems];
CGRect frame = self.view.frame;
frame.size.height -= [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
self.view.frame = frame;
[UIView commitAnimations];
}