Search code examples
iosswiftuitextfielduianimation

How to animate on uitextfields begins editing in swift


I want to perform some animation on secondTextField when the FirstTextfield becomesFirstResponder

After resign first responder the animation should reverse back in second text field. How can I achieve this in swift?

Note: It is different from moving textfield to the top on keyboard appears.

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        if(textField == firstTextField) {
    UIView.animateWithDuration(1.0, animations: {
        self.secTextField.frame = CGRectMake(self.secTextField.frame.origin.x + 500, self.secTextField.frame.origin.y, self.secTextField.frame.size.width, self.secTextField.frame.size.height) })
    }
    return true
}

What I want to do is, when the user types in firstTextField, the secTextField should go and hide out of sight. What actually happens is the secTextField comes from out of sight to its original position


Solution

  • I finally found the answer. I did the following steps.

    1. Clean My project,
    2. Enable and then again disable the Auto-Layouts.
    3. Set the delegate for those textfields both in xib and swift file.
    4. Then the coding:

           `self.secTextField.frame.origin.x = self.secTextField.frame.origin.x + 500`