How to detect when keyboard is shown and hidden
In the ViewDidLoad method of your class set up to listen for messages about the keyboard:
// Listen for keyboard appearances and disappearances[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
Then in the methods you specify (in this case keyboardDidShow
and keyboardDidHide
) you can do something about it:
- (void)keyboardDidShow: (NSNotification *) notif{ // Do something here}- (void)keyboardDidHide: (NSNotification *) notif{ // Do something here}
You may just need addObserver
in viewDidLoad
. But having addObserver
in viewWillAppear
and removeObserver
in viewWillDisappear
prevents rare crashes which happens when you are changing your view.
Swift 4.2
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: UIResponder.keyboardWillHideNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: UIResponder.keyboardWillShowNotification, object: nil)}@objc func keyboardWillAppear() { //Do something here}@objc func keyboardWillDisappear() { //Do something here}override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) NotificationCenter.default.removeObserver(self)}
Swift 3 and 4
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: Notification.Name.UIKeyboardWillHide, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: Notification.Name.UIKeyboardWillShow, object: nil)}@objc func keyboardWillAppear() { //Do something here}@objc func keyboardWillDisappear() { //Do something here}override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) NotificationCenter.default.removeObserver(self)}
Older Swift
override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillAppear:", name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillDisappear:", name: UIKeyboardWillHideNotification, object: nil)}func keyboardWillAppear(notification: NSNotification){ // Do something here}func keyboardWillDisappear(notification: NSNotification){ // Do something here}override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) NSNotificationCenter.defaultCenter().removeObserver(self)}
Swift 3:
NotificationCenter.default.addObserver(self, selector: #selector(viewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)NotificationCenter.default.addObserver(self, selector: #selector(viewController.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)func keyboardWillShow(_ notification: NSNotification){ // Do something here}func keyboardWillHide(_ notification: NSNotification){ // Do something here}