iOS - 更改 2 个 UIView 之间的垂直间距
iOS - Change vertical spacing between 2 UIView
对于单个 XIB 文件,我有 3 个不同的场景。
这是 XIB 文件,我有 3 个不同的 UIView。
我的场景是:
- 显示视图 2,隐藏视图 1 和视图 3。
- 显示视图 1 和视图 3,隐藏视图 2。
- 显示视图 1、视图 2 和视图 3。
我的问题是关于第二种情况,只显示View 1和View 3。
我可以隐藏视图 2,但我希望在这个特定情况下让视图 1 和视图 3 更接近。
我该怎么做?
我尝试过类似的方法但没有成功。
-(void)setConstraints {
[NSLayoutConstraint constraintWithItem:_infoView1
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual
toItem:_infoView3 attribute:NSLayoutAttributeBottom
multiplier:1.0 constant:0];
}
从故事板设置约束:-
1: 视图 2 设置中心(水平和垂直)、前导和尾随(零)和高度根据 Xib 高度的乘数设置。
2:视图 1 设置相等的前导、尾随和视图 2 的高度以及视图 2 的底部 space 为零。
3: View 3 set Equal leading, trailing and height of View 2 and top space from View 2 is zero.
• 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
并根据需要设置颜色。
是否要用非隐藏视图填充父视图,同时保持父视图高度与以前相同??
为 view1、view2 和 view3 分配高度限制。
为这些约束创建出口 - heightConstraintView1、heightConstraintView2 和 heightConstraintView3。
如需隐藏view2,将view2的height constraint设置为你想要的高度即可。
heightConstraintView2.constant = 5
在其他情况下,将所有视图的高度限制设置为相等值。
heightConstraint1.constant = heightConstraint2.constant = heightConstraint3.constant = <A constant value>
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.
我假设所有其他约束都已正确设置。
对于单个 XIB 文件,我有 3 个不同的场景。
这是 XIB 文件,我有 3 个不同的 UIView。
我的场景是:
- 显示视图 2,隐藏视图 1 和视图 3。
- 显示视图 1 和视图 3,隐藏视图 2。
- 显示视图 1、视图 2 和视图 3。
我的问题是关于第二种情况,只显示View 1和View 3。
我可以隐藏视图 2,但我希望在这个特定情况下让视图 1 和视图 3 更接近。
我该怎么做?
我尝试过类似的方法但没有成功。
-(void)setConstraints {
[NSLayoutConstraint constraintWithItem:_infoView1
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual
toItem:_infoView3 attribute:NSLayoutAttributeBottom
multiplier:1.0 constant:0];
}
从故事板设置约束:-
1: 视图 2 设置中心(水平和垂直)、前导和尾随(零)和高度根据 Xib 高度的乘数设置。
2:视图 1 设置相等的前导、尾随和视图 2 的高度以及视图 2 的底部 space 为零。
3: View 3 set Equal leading, trailing and height of View 2 and top space from View 2 is zero.
• 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
并根据需要设置颜色。
是否要用非隐藏视图填充父视图,同时保持父视图高度与以前相同??
为 view1、view2 和 view3 分配高度限制。 为这些约束创建出口 - heightConstraintView1、heightConstraintView2 和 heightConstraintView3。
如需隐藏view2,将view2的height constraint设置为你想要的高度即可。
heightConstraintView2.constant = 5
在其他情况下,将所有视图的高度限制设置为相等值。
heightConstraint1.constant = heightConstraint2.constant = heightConstraint3.constant = <A constant value>
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.
我假设所有其他约束都已正确设置。