为什么 UITextField 在 resignFirstResponder 上设置动画?
Why is UITextField animating on resignFirstResponder?
自 iOS8 起,窗体中的 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];
}
尝试将您的代码包装在此
[UIView performWithoutAnimation:^{
// Changes we don't want animated here
}];
试试这个
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField layoutIfNeeded];
}
我想这应该可以解决您的问题。在 UITextField: When beginning input, textfield bounces up, and then bounces down
有一些类似的 post
如果还有问题请告诉我
问题好像是你在执行
中的那段代码
-(void)keyboardWillShow:(NSNotification *)sender
即使键盘已经处于活动状态,也会导致一些失真。
一个小的解决方法是在调整框架之前检查键盘是否已经激活,如下所示
bool isKeyboardActive = false;
-(void)keyboardWillHide:(NSNotification *)sender
{
self.boxBottomConstraint.constant = 0;
[self.view layoutIfNeeded];
isKeyboardActive = false;
}
-(void)keyboardWillShow:(NSNotification *)sender
{
if (!isKeyboardActive) {
CGRect frame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect newFrame = [self.view convertRect:frame fromView:[[UIApplication sharedApplication] delegate].window];
self.boxBottomConstraint.constant = CGRectGetHeight(newFrame);
[self.view layoutIfNeeded];
isKeyboardActive = true;
}
}
自 iOS8 起,窗体中的 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];
}
尝试将您的代码包装在此
[UIView performWithoutAnimation:^{
// Changes we don't want animated here
}];
试试这个
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField layoutIfNeeded];
}
我想这应该可以解决您的问题。在 UITextField: When beginning input, textfield bounces up, and then bounces down
有一些类似的 post如果还有问题请告诉我
问题好像是你在执行
中的那段代码-(void)keyboardWillShow:(NSNotification *)sender
即使键盘已经处于活动状态,也会导致一些失真。
一个小的解决方法是在调整框架之前检查键盘是否已经激活,如下所示
bool isKeyboardActive = false;
-(void)keyboardWillHide:(NSNotification *)sender
{
self.boxBottomConstraint.constant = 0;
[self.view layoutIfNeeded];
isKeyboardActive = false;
}
-(void)keyboardWillShow:(NSNotification *)sender
{
if (!isKeyboardActive) {
CGRect frame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect newFrame = [self.view convertRect:frame fromView:[[UIApplication sharedApplication] delegate].window];
self.boxBottomConstraint.constant = CGRectGetHeight(newFrame);
[self.view layoutIfNeeded];
isKeyboardActive = true;
}
}