简体   繁体   English

uitextfield动画宽度约束

[英]uitextfield animate width constraint

I have this code to animate a width constraint of a UITextField 我有这个代码来动画UITextField的宽度约束

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

It works fine, the only issue is that if there is text inside the text field, during the animation it shrinks in character width and then re-adjusts to the correct size. 它工作正常,唯一的问题是如果文本字段中有文本,在动画期间它会收缩字符宽度,然后重新调整到正确的大小。 The placeholder text does not have the same issue. 占位符文本没有相同的问题。

Here is a gif: http://makeagif.com/mr4u1D 这是一个gif: http//makeagif.com/mr4u1D

Fahim, you can prevent the text scaling by resigning the first responder before you animate the width change, for example with: Fahim,您可以在为宽度更改设置动画之前通过重新设置第一个响应者来阻止文本缩放,例如:

[self.myTextField resignFirstResponder]

or, more generally: 或者,更一般地说:

[self.view endEditing:YES]

This is happening because field is already focused while doing the animation. 发生这种情况是因为在进行动画时场已经聚焦。 My assumption is that you are doing your animation in: 我的假设是你在做动画:

func textFieldDidBeginEditing(textField: UITextField) 

But you should do it instead in: 但你应该这样做:

func textFieldShouldBeginEditing(textField: UITextField) -> Bool

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM