2012-06-05 16 views
24

Devo controllare, dopo che la tastiera è stata mostrata e il pulsante premuto è stato premuto, quando la tastiera si nasconde. Quale evento viene attivato quando si nasconde la tastiera su iOS? GrazieEvento iOS quando la tastiera si nasconde

+0

http://developer.apple.com/library/ios/search /? q = tastiera + nascondi –

+0

possibile duplicato di [ipad come sapere che la tastiera è stata nascosta] (http://stackoverflow.com/questions/7912246/ipad-how-to-know-keyboard-has-been-hidden) –

risposta

56

Sì Utilizza il seguente

//UIKeyboardDidHideNotification when keyboard is fully hidden 
//name:UIKeyboardWillHideNotification when keyboard is going to be hidden 

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

E il onKeyboardHide

-(void)onKeyboardHide:(NSNotification *)notification 
{ 
    //keyboard will hide 
} 
+1

Si attiverà al momento del licenziamento, non quando la tastiera è completamente nascosta. –

+2

sì, corretto, si prega di controllare la risposta aggiornata, per la notifica completamente nascosta utilizzare 'UIKeyboardDidHideNotification' –

5

È possibile ascoltare uno UIKeyboardWillHideNotification, viene inviato ogni volta che la tastiera viene chiusa.

+7

Per essere precisi, la notifica viene inviata PRIMA che la tastiera venga chiusa. –

+0

@Henri, corretto ... come sto trattando proprio ora. – Morkrom

3

Se vuoi sapere quando l'utente premere il pulsante Fine, è necessario adottare il protocollo UITextFieldDelegate, poi in you Visualizza controller implementare questo metodo:

Swift 3:

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
    // this will hide the keyboard 
    textField.resignFirstResponder() 
    return true 
} 

Se vuoi sapere semplicemente quando la tastiera è visualizzato o si nasconde, utilizzare un Notification:

Swift 3:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: .UIKeyboardWillShow , object: nil) 

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: .UIKeyboardWillHide , object: nil) 

func keyboardWillShow(_ notification: NSNotification) { 
    print("keyboard will show!") 

    // To obtain the size of the keyboard: 
    let keyboardSize:CGSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size 

} 

func keyboardWillHide(_ notification: NSNotification) { 
    print("Keyboard will hide!") 
} 
Problemi correlati