6

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

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

4

4 回答 4

2

I've just been having the exact same issue, and this is the solution that I came to.

Set up a separate method for handling when your textField's keyboard is resigned, and place your self.view.center realignment in there. This way, you can ensure that your textFieldDidEndEditing method is kept separate.

Here's an example if I haven't explained myself properly. Note that when the user taps on a textField, I place a DONE button in the navigation bar (due to it being a numerical keypad), although you can link this method to the DONE button on a normal keyboard:

- (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);
}
于 2012-07-03T21:14:44.727 回答
0

Just check in which text field you are (for example, give them tags). Then according to this information, move or don't move your view.

于 2012-07-02T21:33:22.173 回答
0

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

[[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

于 2012-07-06T09:34:58.620 回答
0

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

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

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

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

然后:

  • 如果 x<0,在 textFieldDidEndEditing 中将控件拉伸到原始位置
  • 在 textFieldDidBeginEditing 中,如果 x>0 则卷起控件
于 2014-10-27T16:50:09.213 回答