Ios NSConstraint.constant在设置后检查其值,以避免再次设置视图动画

Ios NSConstraint.constant在设置后检查其值,以避免再次设置视图动画,ios,objective-c,animation,autolayout,Ios,Objective C,Animation,Autolayout,我有一个UITextfield(电子邮件)和一个UILabel(电子邮件错误消息)-见图 当我输入无效电子邮件时,我通过将约束(与IBOutlet链接-IB中的默认设置22在viewDidLoad中被覆盖为0)从0更改为22,在中的UILabel中设置动画。这很好用 当我输入一封有效的电子邮件时,情况正好相反。这也非常有效 我很难找到一种方法来不在中设置UILabel动画,即使它已经存在 我的代码如下: self.usernameErrorMessage.text = @"Invalid

我有一个
UITextfield
(电子邮件)和一个
UILabel
(电子邮件错误消息)-见图

当我输入无效电子邮件时,我通过将约束(与IBOutlet链接-IB中的默认设置22在viewDidLoad中被覆盖为0)从0更改为22,在中的
UILabel
中设置动画。这很好用

当我输入一封有效的电子邮件时,情况正好相反。这也非常有效

我很难找到一种方法来不在中设置
UILabel
动画,即使它已经存在 我的代码如下:

   self.usernameErrorMessage.text = @"Invalid Email";

    NSLog(@"before constant: %f", self.emailErrorMessageLabelHeightConstraint.constant);
    NSLog(@"before email textfield height: %f", self.usernameErrorMessage.bounds.size.height);

    if (self.emailErrorMessageLabelHeightConstraint.constant != 22) {

        self.emailErrorMessageLabelHeightConstraint.constant = 22;
        [self.view setNeedsUpdateConstraints];

        [self.view layoutIfNeeded];
        [UIView animateWithDuration:0.5 animations:^{

            NSLog(@"after constant: %f", self.emailErrorMessageLabelHeightConstraint.constant);
            NSLog(@"after email textfield height: %f", self.usernameErrorMessage.bounds.size.height);

            [self.view layoutIfNeeded];
        }];
    }
首次运行后输出NSLog&输入无效电子邮件

2014-03-19 14:10:44.085 login[21743:60b] before constant: 0.000000
2014-03-19 14:10:44.086 login[21743:60b] before email textfield height: 0.000000
2014-03-19 14:10:47.341 login[21743:60b] after constant: 22.000000
2014-03-19 14:10:47.341 login[21743:60b] after email textfield height: 22.000000
单击返回文本字段以再次运行上面的代码(这次我希望动画不会发生,因为错误消息
UILabel
已经显示…但它只是动画返回,因为当在if语句中选中常量时仍然是0.000000

第二次运行的输出

2014-03-19 14:15:01.074 login[21743:60b] before constant: 0.000000
2014-03-19 14:15:01.074 login[21743:60b] before email textfield height: 0.000000
2014-03-19 14:15:01.865 login[21743:60b] after constant: 22.000000
2014-03-19 14:15:01.866 login[21743:60b] after email textfield height: 22.000000

听起来好像其他代码一定在将常量设置回0。你确定你没有在
updateConstraints
中重新设置它吗?我刚刚检查了所有的代码和IB,仔细检查了所有的约束等等。我没有看到任何异常,但它现在似乎正在工作。谢谢你关于检查所有内容和n的建议重新设置。它现在似乎正在工作。。。