为什么UITextField在resignFirstResponder上设置动画? [英] Why is UITextField animating on resignFirstResponder?

查看:142
本文介绍了为什么UITextField在resignFirstResponder上设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从iOS 8开始,表单中的UITextField表现得非常奇怪。如果我单击另一个文本字段或按键盘上的Tab键,则输入的文本会向上设置动画,然后快速重新显示。每次加载视图后都会发生这种情况,之后不时发生。

Since iOS 8, UITextFields in a form behave very strangely. If I click an another text field or press Tab on the keyboard, the entered text animates upwards then reappears quickly. It happens every time after the view did loaded, and every now and then afterwards.

它看起来像这样:

我的代码如下所示:

#pragma mark - <UITextFieldDelegate>

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.passwordTextField) {
        [self loginButtonClicked:nil];
    } else if (textField == self.emailTextField) {
        [self.passwordTextField becomeFirstResponder];
    }

    return YES;
}

编辑:

看起来这个问题是由我的键盘监听器引起的:

It looks like this issue is caused by my keyboard listeners:

[[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];
}


推荐答案

问题似乎是你正在执行这段代码

The problem seems to be that you are executing the piece of code in

-(void)keyboardWillShow:(NSNotification *)sender

即使键盘已经激活,也会导致一些失真。

even if the keyboard is already active, which leads to some distortion.

一个小小的工作是在调​​整帧之前检查键盘是否已经激活,如下所示

A small work around would be to check if the keyboard is already active before adjusting the frames, as below

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;
    }
}

这篇关于为什么UITextField在resignFirstResponder上设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆