I have a login screen that gets email and password. the password text field is secured and it results with a different keyboard height. When I switch between the email text field to the password field, without dismissing the keyboard first (clicking on the password while the email keyboard is presented) my frame doesn't recalculate the keyboard height and I get black color in the area of difference between the keyboards.
this is the code that handles the keyboard in my View controller, it is obvious that the problem is that the keyboardWillHide/show
func's are not being called again and calculate the correct height for the frame if they were not dismissed:
private func setupKeyboard(){
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification){
UIView.animate(withDuration: 2.0, animations: {
self.appLogo.alpha = 0
})
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= (keyboardSize.height)
}
}
}
func keyboardWillHide(notification: NSNotification){
self.appLogo.alpha = 1
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += (keyboardSize.height)
}
}
}
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
appLogo.isHidden = false
view.endEditing(true)
}
I am looking for a method that will fix this situation, maybe that when I click on the password field, it will first dissmiss the email field keyboard and then open the password keyboard. Any help appreciated.
keyboardWillHide
why don't you just set the origin back to 0 instead of adding the keyboard height? – paulvs