2017-06-12 54 views
0

對於單個XIB文件,我有3種不同的方案。iOS - 更改2 UIView的垂直間距

這裏是廈門國際銀行文件,我有3個不同的UIView。

enter image description here

我的方案是:顯示

  • 視圖2和視圖1和視圖3被隱藏。
  • 顯示視圖1和視圖3,隱藏視圖2。
  • 顯示視圖1,視圖2和視圖3。

我的問題是關於第二種情況,其中只顯示視圖1和視圖3。

我可以隱藏查看2,但我想對於這種特殊情況下,使視圖1和視圖3密切。

enter image description here

我該怎麼辦呢?

我嘗試過這樣的事情,但沒有成功。從故事板

-(void)setConstraints { 

    [NSLayoutConstraint constraintWithItem:_infoView1 
           attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual 
            toItem:_infoView3 attribute:NSLayoutAttributeBottom 
           multiplier:1.0 constant:0]; 
} 

回答

1
Step 1 : Connect top constraint of view 2 with view 1 similarly connect top constraint of view 3 with view 2 then create and map height constraints for view 1 view 2 and view 3. 

Step 2 : For scenarios mentioned in question, put conditions for your cases and do steps mentioned below 

1) View 2 is displayed and View 1 and View 3 are hidden. 
set height constraint for view 1 and view 3 to zero 
heightConstraintOfView1.constant = 0 
heightConstraintOfView3.constant = 0 

2) View 1 and View 3 are displayed and View 2 is hidden. 
set height constraint for view 2 to zero 
heightConstraintOfView2.constant = 0 

3) View 1, View 2 and View 3 are displayed. 
Don’t set height constraint for any view to zero. 

我承擔所有其他的限制設置正確。

0

集的約束: -

1:查看2設置的中心(水平和垂直),前緣和後(零)和高度設​​置爲根據乘數XIB的高度。

2:查看1設定爲等於領先,尾隨和從View 2查看2和底部空間的高度是零。

3:查看3設定爲等於領先,尾隨和從View 2查看2和頂部空間的高度是零。

• View 2 is displayed and View 1 and View 3 are hidden. 

    View2.isHidden = false 
    View1.isHidden = true 
    View3.isHidden = true 

•顯示視圖1和視圖3,隱藏視圖2。

 View2.isHidden = true 
    View1.isHidden = false 
    View3.isHidden = false 

•顯示視圖1,視圖2和視圖3。

 View2.isHidden = false 
     View1.isHidden = false 
     View3.isHidden = false 

並設置顏色根據需要。

0

你想填補像以前那樣保持父視圖高度相同的非隱藏視圖的父視圖?

0

爲視圖1,視圖2和視圖3分配高度約束。 爲這些約束創建出口 - heightConstraintView1,heightConstraintView2和heightConstraintView3。

對於這種情況,你要隱藏的視圖2,設置視圖2,你想要的高度高度約束。

heightConstraintView2.constant = 5 

在其他情況下,爲所有視圖設置高度約束爲等值。

heightConstraint1.constant = heightConstraint2.constant = heightConstraint3.constant = <A constant value>