2015-11-05 44 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

相關問題