Это довольно распространенная тема, и на нее есть много ответов.
Ситуация : у меня полноэкранный (без панели инструментов) UITextView с UIToolbar внизу. когда 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];
}
В клавиатуре WillShow:
- (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];
}