iOS event when keyboard hides

26,755

Solution 1

Yes Use the following

//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];

And the onKeyboardHide

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

Solution 2

If you want to know when the user press the Done button, you have to adopt the UITextFieldDelegate protocol, then in you View controller implement this method:

Swift 3:

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

If you want to know simply when keyboard is shown or is hiding, use a 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!")
}

Solution 3

You can listen for a UIKeyboardWillHideNotification, it's sent whenever the keyboard is dismissed.

Share:
26,755

Related videos on Youtube

Jaume
Author by

Jaume

Updated on July 14, 2022

Comments

  • Jaume
    Jaume almost 2 years

    I need to control, after keyboard is shown and done button is pressed, when keyboard hides. Which event is triggered when hides keyboard on iOS? Thank you

  • Henri Normak
    Henri Normak almost 12 years
    To be precise, the notification is sent BEFORE the keyboard is dismissed.
  • Andres Canella
    Andres Canella over 10 years
    This will trigger at the moment of dismissal, not when the keyboard is fully hidden.
  • Omar Abdelhafith
    Omar Abdelhafith over 10 years
    yes, correct, please check the updated answer, for fully hidden notification use UIKeyboardDidHideNotification