Uitextfield envelope width limit

I have this code to animate a UITextField width limit

self.myTextFieldWidthConstraint.constant = 200 UIView.animateWithDuration(2, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in self.viewContainer.layoutIfNeeded() }, completion: nil) 

It works great, the only problem is that if there is text in the text box, during the animation it is compressed by the width of the characters, and then it is adjusted to the correct size again. Placeholder text does not have the same problem.

Here is the gif: http://makeagif.com/mr4u1D

+5
source share
2 answers

Fahim, you can prevent the text from scaling by postponing the first responder before you change the width change, for example:

 [self.myTextField resignFirstResponder] 

or, more generally:

 [self.view endEditing:YES] 
0
source

This is because the field is already focused during the animation. My guess is that you are doing your animation in:

 func textFieldDidBeginEditing(textField: UITextField) 

But you should do this instead:

 func textFieldShouldBeginEditing(textField: UITextField) -> Bool 
0
source

Source: https://habr.com/ru/post/1207179/


All Articles