2015-09-27 25 views
1

我在ios的約束部分中確實很弱。我已經通過了許多參考鏈接,如下所示,但仍然無法解決我的問題。在運行時快速更改約束條件

Hide autolayout UIView : How to get existing NSLayoutConstraint to update this one

在情況下,我需要改變基於一些條件UIView2的y軸。 storyboard.png顯示佈局。 enter image description here

如果條件與步驟1失敗UIView2,步驟2和步驟3 應該是可見的,它的位置也應在condition1.png改變如圖

enter image description here

如果條件通過UIView2與步驟1,步驟2和步驟3 應該不可見如condition1.png所示

enter image description here

請爲我提供如何實現上述場景的步驟。我嘗試過,但如果爲UIView1設置約束,高度保持不變,並且在運行時不會改變。

回答

3

只是使雙方的UIView的高度的出口和利用不變的性質改變,如高度限制...

if conditionfails{ 
    heightconstOfView1.constant = 0 
} 
else{ 
    heightconstOfView2.constant = 0 
} 
+0

讓我嘗試將更新它.. – cybergeeeek

+0

確定...如果你有任何問題,讓我知道 –

+0

高度的視圖1的變化,但視圖2完全不動。 – cybergeeeek

0

我用網點上的腳本中設置約束,按鍵之間的div(使用舊的設置動態佈局的方法)。因此,對於大屏幕(iPhone 6及以上版本),我更新固定高度的值(僅在縱向模式下觀看應用程序)。對我來說有效的是:

constHeightlogo.constant = 140 //old value = 72 
///set any other updates here then force the view 
self.view.layoutIfNeeded() 

工作就像一個魅力。

在這裏看到相關:

Update the constant property of a constraint programmatically in Swift?