Swift 3 NSNotificationCenter Keyboardwillshow/hide
On Swift 4.2, addObserver name for NSNotificationCenter changed as well:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardDidShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardDidHideNotification, object: nil)
Check out the updated Swift Programming Language book. Pages 1027 and 1028 are what you're looking for. It should be something like this:
func keyboardWillHide(_ notification: NSNotification) {…
Notice the additional underscore above. Also:
#selector(LoginViewController.keyboardWillHide(_:))
You also might need to add @objc(keyboardWillHideWithNotification:)
to your class.
NSNotificationCenter have things alter for get show keyboard:
NotificationCenter.default.addObserver(self, selector: #selector(NovaVisitaVC.abreTeclado(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NovaVisitaVC.abreTeclado(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
Use that code that's work on swift3
You can use your ViewController (e.g, loginvc
) to add notification
let loginvc : LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
NotificationCenter.default.addObserver(self,
selector: #selector(loginvc.keyboardWillShow(notification:)),
name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(loginvc.keyboardWillHide(notification:)),
name: NSNotification.Name.UIKeyboardWillHide, object: nil)
Then add keyboard hide and show method
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
print("Show")
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
print("Hide")
}
}