Andrew
Andrew

Reputation: 16051

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?

Upvotes: 8

Views: 2828

Answers (2)

dyt
dyt

Reputation: 201

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)

Upvotes: 20

Adil Soomro
Adil Soomro

Reputation: 37729

you could play around with rightViewMode property

textField.rightViewMode = UITextFieldViewModeAlways;

There are following modes available:

UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways

Upvotes: -2

Related Questions