2015-11-05 45 views
15

自iOS 8以来,表单中的UITextFields表现得非常奇怪。如果我点击另一个文本字段或按下键盘上的Tab键,输入的文字将向上移动,然后快速重新出现。它发生在每次视图加载后,以及之后的每一次。为什么UITextField在resignFirstResponder上动画?

它看起来像这样:

我的代码如下所示:

#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]; 
} 
+0

它为我工作得很好。你在使用UITextField的第三方子类吗? –

+0

不,我不知道。这是一个普通的'UITextField',我不使用第三方库或其他。 – gklka

+0

你在模拟器上检查?如果是,你使用外部键盘? –

回答

4

这个问题似乎是您正在执行的代码在

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

尝试在此

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

像什么?我不明白我应该把什么放入非动画块。 – gklka

+0

我假设'[self.view layoutIfNeeded];' – Alistra

相关问题