出现键盘时调整视图的大小(iOS)

我意识到有很多类似的解决scheme,比如TPKeyboardAvoiding , 苹果着名的解决scheme ,以及涉及使用UIScrollView的各种build议。 在我的情况下,我需要调整视图的大小,以适应键盘,而不是滚动或移动它。 这个解决scheme最接近我想要达到的目标,所以这是我的基础。 但是,我有一个问题,使景观模式下工作的事情。 当键盘出现时,我调整视图大小的方法是:

- (void)keyboardWillShow:(NSNotification *)note { NSDictionary *userInfo = note.userInfo; NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; CGRect keyboardFrame = [[self textField].superview convertRect:[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil]; CGRect statusBarFrame = [[self textField].superview convertRect:[UIApplication sharedApplication].statusBarFrame fromView:nil]; CGRect bounds = [self textField].superview.bounds; CGRect newFrame = CGRectMake(0.0, 0.0, bounds.size.width, keyboardFrame.origin.y + statusBarFrame.size.height); [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{ [self textField].superview.frame = newFrame; } completion:nil]; } 

这在肖像模式下完美工作。

在这里输入图像说明

但是,在横向模式下,视图从左到右或从右到左调整,具体取决于设备旋转的方向,而不是从下到上。

在这里输入图像说明

很显然,我使用坐标的方式有些问题,在横向模式下,一些参照系并不是我想的,但是我正在整理如何解决这个问题。 我已经尝试使用-convertRect转换所有types的东西:但是我试图让我无处不在。

我真的希望有一个谁不那么困惑的所有这些矩形,以及如何改变方向的变化可以发现我做错了什么,我需要做什么才能得到这个权利。 作为参考,我创build了一个项目,展示了最简单的情况,重现了我遇到的问题。

我不build议你调整你的视图控制器的根视图的大小,你可以创buildcontentView并添加到视图控制器的视图。 你可以改变这个contentView的大小如下(我不使用自动播放):

 - (void)keyboardWillShow:(NSNotification *)note { NSDictionary *userInfo = note.userInfo; NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil]; [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{ self.contentView.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y); } completion:nil]; } - (void)keyboardWillHide:(NSNotification *)note { NSDictionary *userInfo = note.userInfo; NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil]; [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{ self.contentView.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y); } completion:nil]; } 

Vitaliy B的答案很快。 我有一个名为templateHeaderContentView的视图,我创build了一个函数并在那里configuration了视图高度。 您使用自己的视图,并相应地改变高度。

 func keyboardWillShow(notification: NSNotification) { keyboardShowOrHide(notification) } func keyboardWillHide(notification: NSNotification) { keyboardShowOrHide(notification) } private func keyboardShowOrHide(notification: NSNotification) { guard let userInfo = notification.userInfo else {return} guard let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey]else { return } guard let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] else { return } guard let keyboardFrameEnd = userInfo[UIKeyboardFrameEndUserInfoKey] else { return } let curveOption = UIViewAnimationOptions(rawValue: UInt(curve.integerValue << 16)) let keyboardFrameEndRectFromView = view.convertRect(keyboardFrameEnd.CGRectValue, fromView: nil) UIView.animateWithDuration(duration.doubleValue ?? 1.0, delay: 0, options: [curveOption, .BeginFromCurrentState], animations: { () -> Void in self.templateHeaderContentView.configureView(keyboardFrameEndRectFromView.origin.y) }, completion: nil) } 

我已经做到了,希望这个代码对你有帮助。

 - (void)viewWillAppear:(BOOL)animated { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil]; } - (void)keyboardWillShow:(NSNotification*)notification { [self moveControls:notification up:YES]; } - (void)keyboardWillBeHidden:(NSNotification*)notification { [self moveControls:notification up:NO]; } - (void)moveControls:(NSNotification*)notification up:(BOOL)up { NSDictionary* userInfo = [notification userInfo]; CGRect newFrame = [self getNewControlsFrame:userInfo up:up]; [self animateControls:userInfo withFrame:newFrame]; } - (CGRect)getNewControlsFrame:(NSDictionary*)userInfo up:(BOOL)up { CGRect kbFrame = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; kbFrame = [self.view convertRect:kbFrame fromView:nil]; CGRect newFrame = self.view.frame; newFrame.origin.y += kbFrame.size.height * (up ? -1 : 1); return newFrame; } - (void)animateControls:(NSDictionary*)userInfo withFrame:(CGRect)newFrame { NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; UIViewAnimationCurve animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]; [UIView animateWithDuration:duration delay:0 options:animationOptionsWithCurve(animationCurve) animations:^{ self.view.frame = newFrame; } completion:^(BOOL finished){}]; } static inline UIViewAnimationOptions animationOptionsWithCurve(UIViewAnimationCurve curve) { return (UIViewAnimationOptions)curve << 16; } 

试试这个方法。 根据您的要求编辑它。

 #define kOFFSET_FOR_KEYBOARD 280.0 - (void)keyboardWillHide:(NSNotification *)notif { [self setViewMoveUp:NO]; } - (void)keyboardWillShow:(NSNotification *)notif{ [self setViewMoveUp:YES]; } - (void)textFieldDidBeginEditing:(UITextField *)textField { stayup = YES; [self setViewMoveUp:YES]; } - (void)textFieldDidEndEditing:(UITextField *)textField { stayup = NO; [self setViewMoveUp:NO]; } //method to move the view up/down whenever the keyboard is shown/dismissed -(void)setViewMoveUp:(BOOL)moveUp { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.3]; // if you want to slide up the view [UIView setAnimationBeginsFromCurrentState:YES]; CGRect rect = self.view.frame; if (moveUp) { // 1. move the view's origin up so that the text field that will be hidden come above the keyboard // 2. increase the size of the view so that the area behind the keyboard is covered up. if (rect.origin.y == 0 ) { rect.origin.y -= kOFFSET_FOR_KEYBOARD; //rect.size.height += kOFFSET_FOR_KEYBOARD; } } else { if (stayup == NO) { rect.origin.y += kOFFSET_FOR_KEYBOARD; //rect.size.height -= kOFFSET_FOR_KEYBOARD; } } self.view.frame = rect; [UIView commitAnimations]; }