iOS自动布局与两个视图保持距离
我想在屏幕顶部固定一些视图, 另一些位于底部,并且在顶视图和底视图之间的相等距离中有一个固定大小的视图 我不知道如何使用自动布局约束执行此操作。我是否需要向UI添加一些间隔视图,或者通过编程计算所需的位置iOS自动布局与两个视图保持距离,ios,interface-builder,autolayout,Ios,Interface Builder,Autolayout,我想在屏幕顶部固定一些视图, 另一些位于底部,并且在顶视图和底视图之间的相等距离中有一个固定大小的视图 我不知道如何使用自动布局约束执行此操作。我是否需要向UI添加一些间隔视图,或者通过编程计算所需的位置 只需一个附加视图即可完成此操作。看起来是这样的: stuff_on_top middle_view (with fixed size view inside) stuff_on_bottom 顶部的stuff_和中间视图之间以及底部的中间视图和stuff_之间存在垂直间距限制固定大小视图将在
只需一个附加视图即可完成此操作。看起来是这样的:
stuff_on_top
middle_view (with fixed size view inside)
stuff_on_bottom
顶部的stuff_
和中间视图
之间以及底部的中间视图
和stuff_之间存在垂直间距限制<代码>固定大小视图
将在中间视图
中水平和垂直居中
另一种方法是在两个间隔视图之间放置两个间隔视图:顶部的填充视图和底部的填充视图和底部的填充视图。然后,您需要添加一个约束,使间距视图的高度相等。查看此类别:
然后你可以做一些像这样简单的事情
#import "UIView+AutoLayout.h"
...
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *topView = [UIView autoLayoutView];
UIView *centralContainerView = [UIView autoLayoutView];
UIView *centralView = [UIView autoLayoutView];
UIView *bottomView = [UIView autoLayoutView];
topView.backgroundColor = [UIColor redColor];
bottomView.backgroundColor = [UIColor redColor];
centralView.backgroundColor = [UIColor greenColor];
[self.view addSubview:topView];
[self.view addSubview:centralContainerView];
[centralContainerView addSubview:centralView];
[self.view addSubview:bottomView];
//Pins the topView to the top, left and right edges of its superview (in iOS 7, it uses the topLayoutGuide)
[topView pinToSuperviewEdges:JRTViewPinTopEdge|JRTViewPinLeftEdge|JRTViewPinRightEdge inset:0 usingLayoutGuidesFrom:self];
//Constrains the height of topView to 75pts (if a value is passed as zero, no constrain is applied to that axis)
[topView constrainToSize:CGSizeMake(0, 75)];
//Pins the centralContainerView to the left and right edges of its superview
[centralContainerView pinToSuperviewEdges:JRTViewPinLeftEdge|JRTViewPinRightEdge inset:0];
//Pins the top of centralContainerView to the bottom of topView
[centralContainerView pinEdge:NSLayoutAttributeTop toEdge:NSLayoutAttributeBottom ofItem:topView];
//Pins the bottom of centralContainerView to the top of bottomView
[centralContainerView pinEdge:NSLayoutAttributeBottom toEdge:NSLayoutAttributeTop ofItem:bottomView];
//Centers centralView on the Y axis of its superview
[centralView centerInContainerOnAxis:NSLayoutAttributeCenterY];
//Pins the centralView to the left and right edges of its superview
[centralView pinToSuperviewEdges:JRTViewPinLeftEdge|JRTViewPinRightEdge inset:0];
//Constrains the height of topView to 100pts
[centralView constrainToSize:CGSizeMake(0, 100)];
//Pins the topView to the bottom, left and right edges of its superview (in iOS 7, it uses the bottomLayoutGuide)
[bottomView pinToSuperviewEdges:JRTViewPinBottomEdge|JRTViewPinLeftEdge|JRTViewPinRightEdge inset:0 usingLayoutGuidesFrom:self];
//Constrains the height of topView to 75pts
[bottomView constrainToSize:CGSizeMake(0, 75)];
}
得到如下输出:
stuff_on_top
middle_view (with fixed size view inside)
stuff_on_bottom
编辑:
我没有看到interface builder标签,只是匆匆下结论。。。界面生成器替代方案的工作原理与上述类似。。您需要有三个主视图,一个固定在顶部,另一个固定在底部。。然后,一个具有灵活宽度的视图固定到其他两个视图
然后,您可以在中间视图中将第四个视图置于固定高度的中心。然后,这将为您提供您要查找的结果
但是我认为有一个自动布局的解决方案…你是说中间视图会在俯视图和俯视图之间展开,因为垂直间距限制?没错<顶部的“代码>填充物”和底部的“代码>填充物”大小固定。自动布局也会知道屏幕的高度,因此计算中间视图的高度不会有问题。