iOS - 更改2 UIView之间的垂直间距

时间:2022-06-04 15:21:38

I have 3 different scenarios for a single XIB File.

对于单个XIB文件,我有3种不同的方案。

Here is the XIB File, I have 3 different UIView.

这是XIB文件,我有3个不同的UIView。

iOS  - 更改2 UIView之间的垂直间距

My scenarios are :

我的场景是:

  • View 2 is displayed and View 1 and View 3 are hidden.
  • 显示视图2,隐藏视图1和视图3。
  • View 1 and View 3 are displayed and View 2 is hidden.
  • 显示视图1和视图3,隐藏视图2。
  • View 1, View 2 and View 3 are displayed.
  • 显示视图1,视图2和视图3。

My question is concerning the second case, where only View 1 and View 3 are displayed.

我的问题是关于第二种情况,其中只显示了视图1和视图3。

I can hide View 2, but I would like for this specific case to make View 1 and View 3 closer.

我可以隐藏View 2,但我希望这个特定情况能够使View 1和View 3更接近。

iOS  - 更改2 UIView之间的垂直间距

How can I do it ?

我该怎么做 ?

I tried with something like this but without success.

我试过这样的事但没有成功。

-(void)setConstraints {

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

4 个解决方案

#1


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.

I assume all other constraints are set properly.

我假设所有其他约束都设置正确。

#2


0  

Set constraints from storyBoard :-

从storyBoard设置约束: -

1: View 2 set the center(Horizontal and vertical) , leading and trailing (zero) and height set according multiplier the height of Xib.

1:视图2设置中心(水平和垂直),前导和尾随(零)和高度根据Xib的高度设置。

2: View 1 set Equal leading , trailing and height of View 2 and bottom space from View 2 is zero.

2:视图1设置视图2的相等前导,尾随和高度以及视图2中的底部空间为零。

3: View 3 set Equal leading , trailing and height of View 2 and top space from View 2 is zero.

3:视图3设置视图2的相等前导,尾随和高度以及视图2中的顶部空间为零。

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

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

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

•显示视图1和视图3,隐藏视图2。

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

• View 1, View 2 and View 3 are displayed.

•显示视图1,视图2和视图3。

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

And set color According as required.

并根据需要设置颜色。

#3


0  

Do you want to fill the parent view with the non-hidden views keeping the parent view height same as before ??

是否要使用非隐藏视图填充父视图,以保持父视图高度与之前相同?

#4


0  

Assign the height constraints for view1, view2 and view3. Create the outlets for these constraints - heightConstraintView1, heightConstraintView2 and heightConstraintView3.

为view1,view2和view3指定高度约束。为这些约束创建出口 - heightConstraintView1,heightConstraintView2和heightConstraintView3。

For the case, you want to hide the view2,set the height constraint for view2 as the height you want.

对于这种情况,您希望隐藏view2,将view2的高度约束设置为所需的高度。

heightConstraintView2.constant = 5

In other case, set the height constraint as equal value for all views.

在其他情况下,将高度约束设置为所有视图的相等值。

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

#1


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.

I assume all other constraints are set properly.

我假设所有其他约束都设置正确。

#2


0  

Set constraints from storyBoard :-

从storyBoard设置约束: -

1: View 2 set the center(Horizontal and vertical) , leading and trailing (zero) and height set according multiplier the height of Xib.

1:视图2设置中心(水平和垂直),前导和尾随(零)和高度根据Xib的高度设置。

2: View 1 set Equal leading , trailing and height of View 2 and bottom space from View 2 is zero.

2:视图1设置视图2的相等前导,尾随和高度以及视图2中的底部空间为零。

3: View 3 set Equal leading , trailing and height of View 2 and top space from View 2 is zero.

3:视图3设置视图2的相等前导,尾随和高度以及视图2中的顶部空间为零。

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

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

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

•显示视图1和视图3,隐藏视图2。

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

• View 1, View 2 and View 3 are displayed.

•显示视图1,视图2和视图3。

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

And set color According as required.

并根据需要设置颜色。

#3


0  

Do you want to fill the parent view with the non-hidden views keeping the parent view height same as before ??

是否要使用非隐藏视图填充父视图,以保持父视图高度与之前相同?

#4


0  

Assign the height constraints for view1, view2 and view3. Create the outlets for these constraints - heightConstraintView1, heightConstraintView2 and heightConstraintView3.

为view1,view2和view3指定高度约束。为这些约束创建出口 - heightConstraintView1,heightConstraintView2和heightConstraintView3。

For the case, you want to hide the view2,set the height constraint for view2 as the height you want.

对于这种情况,您希望隐藏view2,将view2的高度约束设置为所需的高度。

heightConstraintView2.constant = 5

In other case, set the height constraint as equal value for all views.

在其他情况下,将高度约束设置为所有视图的相等值。

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