2

I am doing some animation on two UITextFields. When one text field is done with editing, I use UIView.animateWithDuration to animate the first text field to the left, and another call to UIView.animateWithDuration to show the second text field for editing.

The problem is, when I call becomeFirstResponder() on the second text field, it reverts the new x/y coordinates on the first ui text field.

Whats going on?

EDIT

Code provided

func animateMe() {
    UIView.animateWithDuration(0.5, delay: 0, options: .CurveEaseOut, animations: {
        self.onText.frame.offset(dx: self.onText.frame.size.width / -4 * 3 - 10, dy: 0)
        }, completion: { finished in
            println("one shown!")
            self.oneText.frame.offset(dx: self.onText.frame.size.width / -4 * 3 - 10, dy: 0)
            self.twoText.becomeFirstResponder() //undoes the whole animation
    })
}
Tyler
  • 19,113
  • 19
  • 94
  • 151

2 Answers2

0

In the completion block of the UIView animation method, set the frame of your UITextfield to the target new position. The animation will not do that for you, which is counterintuitive but the explanation is a too long.

Earl Grey
  • 7,426
  • 6
  • 39
  • 59
0

If you have used constraints to create the original positions, it is possible that the layout is being refreshed when the keyboard pops up which will revert your text box back to the position the constraints say it should be at.

If this is the case you may want to set the text field final frame inside layoutSubviews. That way you will keep overriding the default layout. Only do this when it is supposed to be collapsed so it pops back when it's supposed to.

Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
  • I have removed all of the constraints from the text fields, and have actually disabled the keyboard (simulator) and the issue still persists – Tyler Mar 02 '15 at 22:03
  • If you have any constraints anywhere, Xcode will add in auto ones behind the scenes. I would add layoutSubviews and just call super but put in a break point so you can detect if it's called. becomeFirstResponder asks the text field for an input view and accessory view then will most likely do layout regardless of if you show the keyboard or not in the simulator. – Rory McKinnel Mar 02 '15 at 22:26