在动画UITextField子类的位置时文本跳转 [英] Text jumps when animating position of UITextField subclass

查看:106
本文介绍了在动画UITextField子类的位置时文本跳转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我为 UITextField 子类的位置设置动画时,该字段中的文本与 UITextField 持有它,这是非常刺耳的,我无法弄清楚为什么会发生:




When I animate the location of my UITextField subclass, the text in the field jumps out of step with the location of the UITextField holding it, which is very jarring and I can't figure out why it happens:

我的子类在右边添加一个按钮轻触的文本字段的手侧调用视图控制器上的方法,该方法更改文本字段边框颜色并通过更改文本字段的顶部空间约束以及位于上方的其他几个视图将文本字段向上移动到屏幕上它。

My subclass adds a button to the right hand side of the text field that when tapped calls a method on the view controller, which changes the textfield border colour and moves the text field up the screen by changing the top space constraint of the text field, and a couple of other views located above it.

[UIView animateWithDuration:0.3
                     animations:^{
                         self.tableViewBottomSpaceConstraint.constant = 0;
                         self.mainLabelHeightConstraint.constant = 0;
                         self.subLabelTopSpaceConstraint.constant = 0;
                         self.postCodeFieldTopSpaceConstraint.constant = 12;
                         [self.view layoutIfNeeded];

                     }];


推荐答案

我修复此问题。将您的代码更改为此。它会起作用。

i fixed this. change your code to this. It will work.

[UIView animateWithDuration:0.3
                 animations:^{
                     self.tableViewBottomSpaceConstraint.constant = 0;
                     self.mainLabelHeightConstraint.constant = 0;
                     self.subLabelTopSpaceConstraint.constant = 0;
                     self.postCodeFieldTopSpaceConstraint.constant = 12;
                     [self.view layoutSubviews];

                 }];

或者如果这不起作用则代替

or if this doesn't work then instead of

[self.view layoutSubviews];

使用文本字段和layoutSubviews的父视图。在我的情况下,这工作。我认为这个答案有资格获得赏金。谢谢。

use the parent view of the text field and layoutSubviews. In my case this worked. I think this answer is eligible for the bounty. Thanks.

编辑:

我找到了原因。当我们尝试layoutIfNeeded时,它意味着它首先更改外部布局,然后更改其布局后更改UITextField的内部文本,因为我们没有强制更改该文本字段的所有子视图的布局。毕竟UITextField由UIView和Label以及其他基本元素组成。但是当我们尝试layoutSubviews同时改变所有子视图的布局时。这就是为什么弹跳消失了。

I found the reason of this. When we are trying to layoutIfNeeded it means its changing the outer layout first and after it changes its layout its changing the inner text of UITextField because we are not forcefully changing layout of all subviews of that text field. After all UITextField is made up of UIView and Label and other base elements. But when we trying to layoutSubviews its changing layout of all subviews simultaneously. Thats why the bouncing is gone.

这篇关于在动画UITextField子类的位置时文本跳转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆