2016-11-14 48 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示例的答案。 –

相關問題