iOS Autolayout与2个视图保持距离

我想要在屏幕的顶部固定一些视图,在顶部视图和底部视图之间的平等距离上固定一些视图的底部和一个固定大小的视图。

我无法弄清楚如何用Autolayout约束来做到这一点。 我是否需要向用户界面添加一些间隔视图,或以编程方式计算所需的位置?

在这里输入图像说明

只有一个额外的视图可以做到这一点。 它看起来像这样:

stuff_on_top middle_view (with fixed size view inside) stuff_on_bottom 

stuff_on_topmiddle_view之间以及在middle_viewstuff_on_bottom之间会存在垂直间隔约束。 fixed size view将在middle_view水平和垂直middle_view

这样做的另一种方式是两个放置两个spacer视图:在stuff_on_topmiddle_view之间以及在middle_viewstuff_on_bottom之间。 那么你会添加一个约束,间距视图的高度是相等的。

看看这个类别: https : //github.com/jrturton/UIView-Autolayout

那么你可以做一些简单的事情…

 #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)]; } 

你得到这样的输出:

输出4“设备

编辑:

我没有看到接口生成器标签,只是跳到结论…一个接口生成器的替代将类似于上述..你需要有三个主视图,一个固定到顶部,另一个固定到底部然后用一个灵活的宽度固定到另外两个视图。

然后,您可以在中间视图中将具有固定高度的第四个视图居中。 这会给你你正在寻找的结果

我编码这个https://github.com/damienromito/UIView-AutoYPositioning

但我认为与AutoLayout的解决scheme存在…