2017-06-12 87 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>