【问题标题】:textFieldDidEndEditing moves the screen when not wantedtextFieldDidEndEditing 在不需要时移动屏幕
【发布时间】:2012-07-03 07:07:12
【问题描述】:

在我的应用程序中,我的 UITextField 位于键盘顶部的下方,因此我必须向上移动视图,以便在编辑时文本字段在键盘上可见。为此,我使用 UITextFieldDelegate 方法 textFieldDidBeginEditing:。然后在方法 textFieldDidEndEditing: 中,我将视图向下移动。

当我尝试在文本字段之间切换时出现问题。当我这样做时,委托调用方法 textFieldDidEndEditing: 然后立即为另一个文本字段调用 textFieldDidBeginEditing: ,这使视图下降然后立即上升,因此看起来屏幕震动。这种效果有什么解决方法吗?

【问题讨论】:

    标签: iphone objective-c uitextfield


    【解决方案1】:

    我刚刚遇到了完全相同的问题,这就是我找到的解决方案。

    设置一个单独的方法来处理您的 textField 的键盘退出时的处理,并将您的 self.view.center 重新对齐放在那里。这样,您可以确保您的 textFieldDidEndEditing 方法保持独立。

    如果我没有正确解释自己,这里有一个例子。请注意,当用户点击 textField 时,我会在导航栏中放置一个 DONE 按钮(因为它是一个数字键盘),尽管您可以将此方法链接到普通键盘上的 DONE 按钮:

    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        UIBarButtonItem *hideKeyboardButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(resignResponder:)];
        [self.navigationItem setRightBarButtonItem:hideKeyboardButton animated:YES];
    }
    
    - (void)textFieldDidEndEditing:(UITextField *)textField {
    
    //Nothing needs to go here anymore
    
    }
    
    - (void)resignResponder:(id)sender {
        [textField resignFirstResponder];
        //Use core animation instead of the simple action below in order to reduce 'snapback'
        self.view.center = CGRectMake(0, 0, 320, 480);
    }
    

    【讨论】:

    • 这是个好主意,我之前没有想到 - 但是,当文本视图成为第一响应者时,我应该使用哪种方法将视图向上移动?谢谢
    • 在 textFieldDidBeginEditing 方法中。如果你想让它更流畅一点,你可以这样做:CGRect newFrame = CGRectMake(0, -(textField.frame.origin.y - 40), 320, 480); [UIView animateWithDuration:0.3 animations:^{self.view.frame = newFrame;}];
    • 有 1 个问题,当我把它放在那里时,调用了 textFieldDidBeginEditing 方法,但没有调用 textFieldDidEndEditing。因此,每当我开始编辑另一个文本字段时,视图就会一遍又一遍地向上移动。
    • 如果您查看我在之前的评论中建议的代码,我确保新 CGRect 的 Y 坐标在使用 textField 时是主观的 - 因此 UIView 应该动态重新调整。即CGRectMake(0, -(textField.frame.origin.y - 40), 320, 480); - 其中“textField”是通用方法变量(适用于当前使用的 UITextField)
    • 不用担心。感谢您接受这个作为答案。如果您也可以投票给我的答案,那就太好了。
    【解决方案2】:

    只需检查您所在的文本字段(例如,给他们tags)。然后根据此信息,移动或不移动您的视图。

    【讨论】:

    • 我应该在哪里实现它?谢谢
    • 正如您所做的那样,正是在这些委托方法中。他们将文本字段传递给方法,因此您可以检查。
    【解决方案3】:

    我认为移动文本字段的最佳方式是注册通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    

    在keyboardWillShow:&keyboardWillHide:中修改textfield frame

    【讨论】:

      【解决方案4】:

      我使用了不同的方法来解决这个问题: 当从一个编辑控件切换到另一个(field1 -> field2)时,您会收到以下消息:

      • textFieldShouldBeginEditing (field2)
      • textFieldShouldEndEditing (field1)
      • textFieldDidEndEditing (field1)
      • textFieldDidBeginEditing (field2)

      我所做的是使用计数器变量 (x):

      • 在 textFieldShouldBeginEditing 中添加 x+=1
      • 在 textFieldShouldEndEditing 中添加 x-=1

      然后:

      • 在 textFieldDidEndEditing 中如果 x 将控件拉伸到原始位置
      • 在 textFieldDidBeginEditing 中如果 x>0 则卷起控件

      【讨论】:

        猜你喜欢
        • 2015-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-03
        • 1970-01-01
        相关资源
        最近更新 更多