Keyboard animation in iOS

676 Views Asked by At

In my Login view controller i use UITextFieldDelegate and animation to up view when keyboard is open. Code:

in viewWillAppear()

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)

in ViewController

func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true;    
}

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardSize =  (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            kbHeight = keyboardSize.height - 30
            imageLogo.alpha = 0.0
            self.animateTextField(true)
        }
    }
}
func animateTextField(up: Bool) {
    var movement = (up ? -kbHeight : kbHeight)

    UIView.animateWithDuration(0.3, animations: {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement)
    })
}
func keyboardWillHide(notification: NSNotification) {
    imageLogo.alpha = 1.0
    self.animateTextField(false)
}
func DismissKeyboard(){
    self.view.endEditing(true)
}

When i change language in keyboard animation runs again How can i fixed it ???

Thanks...

1

There are 1 best solutions below

0
On BEST ANSWER

I have two or more UITextFields in on View Controller For each UITextField i add myTextField.delegate = self

In this situation if you use animation for hide/show keyboard it is not work good

Solutions: Put .delegate = self only for first text field and it is work good