iOS的自动布局保持从2次距离(iOS Autolayout keep distance from

2019-09-29 14:42发布

我想有固定在屏幕的顶部,其他一些在底部并在顶部和底部视图之间的距离相等的单一固定大小的视图一些看法。

我无法弄清楚如何使用自动布局的限制做到这一点。 我需要一些间隔添加视图UI,或者编程计算所需的位置?

Answer 1:

你可以只用一个附加视图做到这一点。 它会是这样的:

stuff_on_top
middle_view (with fixed size view inside)
stuff_on_bottom

有会是之间垂直间距约束stuff_on_topmiddle_view之间middle_viewstuff_on_bottomfixed size view将在水平和垂直居中middle_view

这样做将是两个其他的方式把两个间隔的观点:之间stuff_on_topmiddle_view之间middle_viewstuff_on_bottom 。 然后你会添加一个约束的间距意见高度相等。



Answer 2:

看看这个类别: 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图。 那么这会给你你正在寻找的结果



Answer 3:

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

但我认为有自动版式的解决方案存在...



文章来源: iOS Autolayout keep distance from 2 views