如何以编程方式更改从故事板添加的约束? [英] How to change constraints programmatically that is added from storyboard?

查看:106
本文介绍了如何以编程方式更改从故事板添加的约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个屏幕。它将显示如下

I have one screen. It will display like below

现在点击用户我有一个帐户和密码(按钮),它将显示如下

Now When User clicked I have an Account and Password(button) it will display like below

我想相应地移动两个视图
我使用storyboard添加约束。现在需要更改编程中的约束..

I want to move both views accordingly I added constraints using storyboard.Now need to change constraints from programming..

推荐答案

您需要创建约束的IBOutlet。

You need to create an IBOutlet of your constraint.

然后在代码中设置约束的常量值:

Then you set the constant value of your constraint in code:

labelWidthConstraint.constant = newValue

如果你想要它的动画,你可以这样做:

If you want it animated you can do something like this:

labelWidthConstraint.constant = newValue
UIView.animate(withDuration: 0.3, animations: { 
    view.layoutIfNeeded()
}



目标-C



Objective-C

self.labelWidthConstraint.constant = newValue;
[UIView animateWithDuration:0.3 animations:^{        
    [self.view layoutIfNeeded];
}];

这篇关于如何以编程方式更改从故事板添加的约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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