iOS 8以来,在表单中,UITextFields的行为非常奇怪。如果我点击另一个文本字段或按下键盘上的Tab键,则输入的文本会向上动画,然后迅速重新出现。每次在视图加载后都会发生这种情况,以及之后的某个时间。
它看起来像这样:
我的代码如下:
#pragma mark - <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.passwordTextField) {
[self loginButtonClicked:nil];
} else if (textField == self.emailTextField) {
[self.passwordTextField becomeFirstResponder];
}
return YES;
}
编辑:
看起来这个问题是由我的键盘监听器引起的:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
- (void)keyboardWillHide:(NSNotification *)sender
{
self.loginBoxBottomLayoutConstraint.constant = 0;
[self.view layoutIfNeeded];
}
- (void)keyboardWillShow:(NSNotification *)sender
{
CGRect frame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect newFrame = [self.view convertRect:frame fromView:[[UIApplication sharedApplication] delegate].window];
self.loginBoxBottomLayoutConstraint.constant = CGRectGetHeight(newFrame);
[self.view layoutIfNeeded];
}