【问题标题】:Why is UITextField animating on resignFirstResponder?为什么 UITextField 在 resignFirstResponder 上设置动画?
【发布时间】:2015-11-05 12:03:17
【问题描述】:

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

【问题讨论】:

  • 它对我来说很好用。你在使用 UITextField 的第三方子类吗?
  • 不,我不知道。这是一个普通的UITextField,我不使用第 3 方库之类的。
  • 你在检查模拟器吗?如果是,您是否使用外接键盘?
  • 在模拟器和真机上都是一样的,不管有没有键盘。
  • 您能否创建一个带有文本字段的示例项目,因为它真的很难理解可能是什么原因。

标签: ios objective-c animation uitextfield resignfirstresponder


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    试试这个

     - (void)textFieldDidEndEditing:(UITextField *)textField
     {
        [textField layoutIfNeeded]; 
     }
    

    我想这应该可以解决您的问题。在UITextField: When beginning input, textfield bounces up, and then bounces down得到了一些类似的帖子

    IOS8 Text in TextField Bounces on Focus

    如果我们还有问题,请告诉我

    【讨论】:

      【解决方案3】:

      尝试将您的代码包装在此

      [UIView performWithoutAnimation:^{
      // Changes we don't want animated here
      }];
      

      【讨论】:

      • 像什么?我不明白我应该在非动画块中放入什么。
      • 我假设[self.view layoutIfNeeded];
      猜你喜欢
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多