2016-11-14 180 views
8

我有一个屏幕。它会显示类似下面如何从故事板中以编程方式更改约束条件?

enter image description here

现在,当用户单击我有一个帐户和密码(按钮)会显示如下下面

enter image description here

我想相应移动两种观点 我使用故事板添加了约束。现在需要更改编程中的约束条件。

+2

只需创建您想要更改的约束插座,就像创建UIButton或UILabel插座一样。 –

回答

18

您需要创建约束的IBOutlet。
enter image description here

然后你在代码中设置的约束的恒定值:

labelWidthConstraint.constant = newValue 

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

斯威夫特

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

Objective-C

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

您不应该调用'layoutSubviews()'(请参阅文档)。改为使用'layoutIfNeeded()'。 – clemens

+0

你能否将此代码转换为目标c? –

+0

Nilam Pari,我更新了包含Objective-C示例的答案。 –