什么是我做错了在退出键盘时在iOS中调整我的视图

我有我的viewController 6 Textfields。 第一行是2,第二行是1,第三行是1,最后一行是2。 只要input文本字段就可以正常工作。 出现键盘时,我只是遇到问题而向上和向下滚动视图。 视图滚动正常,但当我“辞职响应者”, 视图向下滚动,但向下滚动太多。 例如,如果视图向上移动了一个数量,那么一旦我退出响应者,视图将向下移动一个大于y的值。

我有下面的代码设置(我已经提供了额外的代码,以防万一,但我相信问题是在setViewMovedUp方法,但我可能是错的):

键盘显示:

-(void)keyboardWillShow:(NSNotification*)sender { // Animate the current view out of the way NSDictionary* info = [sender userInfo]; kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; if (!didViewMove) { didViewMove = YES; if (self.view.frame.origin.y >= 0) { [self setViewMovedUp:YES]; } else if (self.view.frame.origin.y < 0) { [self setViewMovedUp:NO]; } } } -(void)keyboardWillHide { [UIView animateWithDuration:0.25 animations:^{ self.view.frame = CGRectMake(0, _top, 320, self.view.frame.size.height); }]; return; didViewMove = NO; if (self.view.frame.origin.y >= 0) { [self setViewMovedUp:YES]; } else if (self.view.frame.origin.y < 0) { [self setViewMovedUp:NO]; } } 

调整视图

  -(void)setViewMovedUp:(BOOL)movedUp { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.3]; // slide up the view int desiredHeight = (480 - kbSize.height) / 2; int requiredHeightAdjustment; if (didViewMove) { requiredHeightAdjustment = abs(desiredHeight - (self.view.frame.origin.y + activeField.frame.origin.y)); } else { requiredHeightAdjustment = abs(desiredHeight - (activeField.frame.origin.y )); // requiredHeightAdjustment = abs(desiredHeight - (self.view.frame.origin.y + activeField.frame.origin.y)); } CGRect rect = self.view.frame; if (movedUp) { // 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. rect.origin.y -= requiredHeightAdjustment; rect.size.height += requiredHeightAdjustment; } else { // revert back to the normal state. rect.origin.y += requiredHeightAdjustment; rect.size.height -= requiredHeightAdjustment; } self.view.frame = rect; [UIView commitAnimations]; } 

辞职响应者

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; if ([_cardnumbertext isFirstResponder] && [touch view] != _cardnumbertext) { [_cardnumbertext resignFirstResponder]; } [super touchesBegan:touches withEvent:event]; UITouch *touch1 = [[event allTouches] anyObject]; if ([_salesamounttext isFirstResponder] && [touch1 view] != _salesamounttext) { [_salesamounttext resignFirstResponder]; } [super touchesBegan:touches withEvent:event]; UITouch *touch2 = [[event allTouches] anyObject]; if ([_referencetext isFirstResponder] && [touch2 view] != _referencetext) { [_referencetext resignFirstResponder]; } [super touchesBegan:touches withEvent:event]; UITouch *touch3 = [[event allTouches] anyObject]; if ([_datetext isFirstResponder] && [touch3 view] != _datetext) { [_datetext resignFirstResponder]; } [super touchesBegan:touches withEvent:event]; UITouch *touch4 = [[event allTouches] anyObject]; if ([_timetext isFirstResponder] && [touch4 view] != _timetext) { [_timetext resignFirstResponder]; } [super touchesBegan:touches withEvent:event]; UITouch *touch5 = [[event allTouches] anyObject]; if ([_reasontext isFirstResponder] && [touch5 view] != _reasontext) { [_reasontext resignFirstResponder]; } [super touchesBegan:touches withEvent:event]; } 

最后build立活跃的领域

 - (void)textFieldDidBeginEditing:(UITextField *)textField { activeField = textField; } - (void)textFieldDidEndEditing:(UITextField *)textField { previousField = activeField; } 

我所做的是做最后的数额,我滚动了一个实例variables。 我也保存键盘通知的持续时间和时间曲线设置。

然后,当用户完成编辑,我得到通知键盘正在消失,我可以移动我的观点,通过保存量的倒转,我把视图放在首位。

我build议使用这种方法。

我有一个github上的示例项目,详细logging了这个方法:

Github上的RandomBlobs项目

在README中查找标题为“杂项技术”的部分。 寻找一个链接“滑动您的意见,为键盘腾出空间”。 这是工作代码使用我上面描述的技术。