当我辞职时,在iOS中调整我的观点有什么不对

当我辞职时,在iOS中调整我的观点有什么不对,ios,objective-c,uitextfield,textfield,keyboard-events,Ios,Objective C,Uitextfield,Textfield,Keyboard Events,我的viewController中有6个文本字段。第一行2个,第二行1个,第三行1个,最后一行2个。文本字段可以很好地进行输入。我只是在键盘出现时上下滚动视图有问题。视图向上滚动很好,但当我“退出响应程序”时,视图向下滚动,但向下滚动太多。例如,如果视图向上移动了y量,一旦我退出响应程序,视图将向下移动一个大于y的值 我设置了以下代码(我提供了额外的代码以防万一,但我相信问题出在setViewMovedUp方法中,但我可能错了): 键盘显示: -(void)keyboardWillShow:(N

我的viewController中有6个文本字段。第一行2个,第二行1个,第三行1个,最后一行2个。文本字段可以很好地进行输入。我只是在键盘出现时上下滚动视图有问题。视图向上滚动很好,但当我“退出响应程序”时,视图向下滚动,但向下滚动太多。例如,如果视图向上移动了y量,一旦我退出响应程序,视图将向下移动一个大于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)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];
}
最后,设置活动字段

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

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

我所做的是使我向上滚动的最后数量成为一个实例变量。我还保存键盘通知中的持续时间和计时曲线设置

然后,当用户完成编辑并收到键盘即将离开的通知时,我只需将视图的移动量与我第一次将视图向上移动时的保存量相反

我建议使用这种方法

我在github上有一个示例项目,详细记录了这种方法:


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

我建议使用
UIScrollView
并滚动它。(如果你不想让用户滚动它,就不允许滚动。)在
keyboard willhide
方法中,你在第二行返回,它永远不会通过
if else
语句。onkeyboard hide。。将scrollView内容偏移设置为(0,0)。此外,你不需要做这么多的逻辑上下滚动。看看这个如何正确地管理键盘功能的输入字段@faiziii这是否意味着我必须将我的视图更改为UIScrollView?为什么你否决了我的答案?我建议采用一种行之有效的方法。对不起,这可能是个错误。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}

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