Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop the animation of UITextField.rightView?

On my UITextField the rightView property animates in (slides in) from the left when it's shown.

I'm unsure how to stop this behaviour from happening?

like image 877
Andrew Avatar asked Sep 17 '13 15:09

Andrew


1 Answers

I had this happen recently on iOS 8.

It turns out that the initial position of the rightView was at (0,0) relative to the UITextField, and when it is being displayed for the first time, it is then set to its correct location. If the keyboard is being displayed at the same time, the position of the rightView somehow get animated together with the animation of the keyboard. You can see this clearly in the simulator if you turn on "Slow Animations" ( + T).

The solution is to set the rightView to its final position before the keyboard animation starts. For example, in viewDidLoad or viewWillAppear, call:

textField.rightView.frame = [textField rightViewRectForBounds:textField.bounds]

Swift:

textField.rightView?.frame = textField.rightViewRect(forBounds: textField.bounds)
like image 161
dyt Avatar answered Nov 10 '22 19:11

dyt