2015-10-27 18 views
5

Considerate questo scenario, ho una visualizzazione testuale con tastiera Dismiss impostata in modo interattivo nello storyboard, quindi quando l'utente scorre verso il basso e in grado di ignorare la tastiera in modo interattivo. Ho dei vincoli sulla vista testo in basso per assicurarmi che sia sempre completamente visualizzato nella vista.Come rilevare le modifiche al frame della tastiera quando si chiude la tastiera in modo interattivo?

Il problema attuale è che quando l'utente scorre gradualmente verso il basso per chiudere la tastiera, non riesco a rilevare le modifiche al frame della tastiera. Ho provato UIKeyboardWillHideNotification e UIKeyboardWillChangeFrameNotification, sono stati chiamati solo dopo che la tastiera è stata chiusa.

Quindi la mia domanda è, come possiamo rilevare simultaneamente le modifiche al frame della tastiera quando si chiude la tastiera in modo interattivo?

risposta

5

Non modificare l'altezza del textView per adattarla a tutte le viste. Invece - dovresti cambiare il campo contentInset in modo che la tua TextView rimarrà alla stessa altezza e non dovrai preoccuparti del frame di tracciamento della tastiera interattiva. Vedere la risposta qui: How do I scroll the UIScrollView when the keyboard appears?

+0

Provalo! Mi ha risparmiato un sacco di tempo :) – quarezz

+0

sì, questo è il modo corretto per farlo –

-2

nel metodo viewDidLoad aggiungere queste righe:

[[NSNotificationCenter defaultCenter] addObserver:self 
            selector:@selector(keyboardWillShow:) 
             name:UIKeyboardWillShowNotification 
             object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillHide:) 
              name:UIKeyboardWillHideNotification 
              object:nil]; 

il aggiungere questi metodi per il vostro viewController

- (void)keyboardWillShow:(NSNotification *)notification 
{ 
    NSDictionary *userInfo = [notification userInfo]; 
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    [UIView animateWithDuration:0.30 
          delay:0.0 
         options:(7 << 16) // This curve ignores durations 
        animations:^{ 
         self.buttonsBottomConstraint.constant = keyboardSize.height - self.footerView.bounds.size.height + 4.0; 
         [self.view layoutIfNeeded]; 
        } 
        completion:nil]; 
} 

- (void)keyboardWillHide:(NSNotification *)notification 
{ 
    [UIView animateWithDuration:0.30 
          delay:0.0 
         options:(7 << 16) // This curve ignores durations 
        animations:^{ 
         self.buttonsBottomConstraint.constant = 0.0; 
         [self.view layoutIfNeeded]; 

        } 
        completion:nil]; 


} 
5

Se si desidera osservare telaio tastiera cambia anche quando la tastiera è che si viene trascinato può utilizzare questo: https://github.com/brynbodayle/BABFrameObservingInputAccessoryView

Fondamentalmente si crea una vista di input segnaposto per tastiera (che si attacca alla tastiera tutto il tempo, anche mentre trascinamento) e si osservano le modifiche al frame. Queste modifiche vengono restituite in un blocco, in modo da ottenere sempre il frame corrente della tastiera.

Problemi correlati