2015-11-05 18 views
15

Da iOS 8, i campi UIText in una forma si comportano in modo molto strano. Se clicco su un altro campo di testo o premo Tab sulla tastiera, il testo inserito si anima verso l'alto e poi riappare rapidamente. Succede ogni volta dopo che la vista è stata caricata e ogni tanto in seguito.Perché UITextField si anima su resignFirstResponder?

Ecco come si presenta:

Il mio codice è simile al seguente:

#pragma mark - <UITextFieldDelegate> 

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

    return YES; 
} 

EDIT:

Sembra che questo problema è causato dai miei ascoltatori della tastiera:

[[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

sua funziona bene per me. Stai usando qualche sottoclasse di terze parti di UITextField? –

+0

No, non lo faccio. È un semplice 'UITextField', e io non uso nessuna libreria di terze parti o altro. – gklka

+0

Stai controllando su Simulator? Se sì stai usando una tastiera esterna? –

risposta

4

Il problema sembra essere che si sta eseguendo il pezzo di codice in

-(void)keyboardWillShow:(NSNotification *)sender 

anche se la tastiera è già attiva, il che porta ad alcune distorsioni.

Un piccolo lavoro in giro sarebbe quello di controllare se la tastiera è già attivo prima di regolare i fotogrammi, come di seguito

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

Prova avvolgendo il codice in questo

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

Ti piace cosa? Non capisco cosa dovrei inserire nel blocco non animato. – gklka

+0

Suppongo che '[self.view layoutIfNeeded];' – Alistra

Problemi correlati