且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何以编程方式更改从故事板添加的约束?

更新时间:2023-02-12 17:17:53

您需要创建约束的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];
}];