Для приложения, которое я разрабатываю для iPad, у меня есть режим прокрутки с некоторыми текстовыми полями / текстовыми представлениями в нем. Чтобы все оставалось видимым, я настраиваю свойство contentSize
представления прокрутки, чтобы добавить буфер внизу, который соответствует тому, насколько клавиатура перекрывает представление прокрутки. Вот код (здесь есть кое-что, относящееся к конкретному приложению, но, надеюсь, не настолько, чтобы вы не могли понять его):
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self name:nil object:nil];
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSValue *animationCurve = [[aNotification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey];
UIViewAnimationCurve curve;
[animationCurve getValue:&curve];
NSValue *animationDuration = [[aNotification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval duration;
[animationDuration getValue:&duration];
NSValue *endingFrame = [[aNotification userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect frame;
[endingFrame getValue:&frame];
[UIView beginAnimations:@"keyboardWillShow" context:bodyView];
[UIView setAnimationCurve:curve];
[UIView setAnimationDuration:duration];
// Re-draw code here.
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
NSValue *animationCurve = [[aNotification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey];
UIViewAnimationCurve curve;
[animationCurve getValue:&curve];
NSValue *animationDuration = [[aNotification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval duration;
[animationDuration getValue:&duration];
[UIView beginAnimations:@"keyboardWillHide" context:bodyView];
[UIView setAnimationCurve:curve];
[UIView setAnimationDuration:duration];
// Re-draw code here
[UIView commitAnimations];
}
Мой вопрос таков: что мне делать с размером клавиатуры во время вращения? Я не получаю никаких уведомлений с клавиатуры при повороте iPad, но размер клавиатуры значительно меняется. В идеале я бы просто отрегулировал высоту свойства contentSize
на величину перекрытия клавиатуры в альбомном режиме, но я могу ' Я не вижу хорошего способа сделать это без жесткого кодирования высоты клавиатуры в обеих ориентациях, чего я не хочу делать.