视图未扩展以匹配给定的约束

Views not expanding to match the constraints given

我正在学习 UIAutolayout。在这里,我学习了如何向父视图添加视图,然后提供约束。我计划在屏幕上有两个视图,给它们红色和蓝色 colours.As 约束同时影响父视图和子视图(如格式字符串所示)我已将约束添加到父视图。(是这错了吗???)我在 viewDidLoad 函数中输入了以下代码。

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


UIView * view1 = [[UIView alloc] init];
[view1 setBackgroundColor:[UIColor redColor] ] ;


UIView *view2 = [[UIView alloc] init];
[view2 setBackgroundColor: [UIColor blueColor]] ;


//Adding the view to the main view.
[self.view addSubview:view1] ;

[self.view addSubview:view2] ;


[view1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[view2 setTranslatesAutoresizingMaskIntoConstraints:NO];

//Making the namemap for sending to the function
NSDictionary *nameMap = @{@"view1":view1,@"view2":view2} ;


//Creating the constraint below

NSArray * horizontalContraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[view2]-60-[view1]-30-|" options:0 metrics:nil views:nameMap] ;

NSArray *view1Height = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[view1]-30-|" options:0 metrics:nil views:nameMap] ;

NSArray *view2Height = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[view2]-30-|" options:0 metrics:nil views:nameMap] ;


//Adding the constraints
[self.view addConstraints:horizontalContraints] ;

[self.view addConstraints:view2Height] ;

[self.view addConstraints:view1Height] ;

问题:我在屏幕上只得到蓝色视图作为输出。我没有得到红色 one.I 已经在 iPad 和 iPhone 模拟器上测试了这个,但结果是一样的。没有更多的代码可以提供。我正在使用 Xcode 5 和 iOS 7(好吧,因为我正在学习)。谢谢

好吧,就像你很伤心你正在学习,所以我给你那个教程:AutoLayout Objective-C

如果这不能回答您的问题,请随时问我 :D

编辑:

我之前的回答假设 view1view2 没有水平约束来将它们分开(但事实证明它们就位)。

这里的实际问题是,当您添加 horizontalConstraints 时,这就是您告诉自动版式要做的事情:

从左边开始,留下一个30的space,加上view2,然后留下一个60的space,然后加上view1,最后留下一个space,共 30 个,从右边距开始。

但是这里没有任何内容告诉自动布局需要多宽才能保持视图(view1view2)。即使在创建视图时,也没有为其设置任何框架(这意味着没有高度或宽度)。

现在,当您的视图根据这些约束进行布局时,自动版式只会为您的 horizontalConstraints 中的第一个视图提供总可用宽度,在您的情况下为 view2(因为没有人已告诉自动布局保留 view1 的任何宽度,很高兴将其设置为零)。因此你永远不会看到你的 view1.

您可以采取一些措施来修复它:

  • 要么为两个视图设置固定宽度
  • 为其中一个视图设置固定宽度,然后剩余的宽度将通过自动布局分配给第二个视图。
  • 如果你想让两个视图保持同样的宽度,那么为它们添加一个等宽的约束。然后 autolayout 会将可用宽度平均分配给 view1view2.

原始答案:

问题其实很简单。您正在向 self.view.

添加 2 组约束 view1Heightview2Height

如果你仔细观察它们,这就是你告诉自动布局要做的事情:

对于视图 1: 从顶部开始 self.view 留下 30 的 space,然后添加 view1 并再次留下 30 的 space。

与 view2 类似:从顶部开始在 self.view 中留下 30 的 space,然后添加 view2 并再次留下 30 的 space。

所以实际上您将两个视图放在完全相同的位置。因此现在只有 z 坐标将决定哪个视图可见。由于您在添加 view1 之后添加了 view2(蓝色的),因此它与 view1 重叠。这导致....您猜对了,只有 view2 可见。

问题出在您的 "horizontalContraints" 上。正如 paresh 所说,您需要指定视图的宽度。

因此,将 "horizontalContraints" 更改为

NSArray *horizontalContraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[view2]-60-[view1(==view2)]-30-|" options:0 metrics:nil views:dict];

会给你想要的输出。

你的整改后如下。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIView * view1 = [[UIView alloc] init];
    [view1 setBackgroundColor:[UIColor redColor]];

    UIView *view2 = [[UIView alloc] init];
    [view2 setBackgroundColor:[UIColor blueColor]];

    [view1 setTranslatesAutoresizingMaskIntoConstraints:NO];
    [view2 setTranslatesAutoresizingMaskIntoConstraints:NO];

    //Adding the view to the main view.
    [self.view addSubview:view1];
    [self.view addSubview:view2];

    NSDictionary *dict =[[NSDictionary alloc] init];
    dict = @{@"view1":view1, @"view2":view2};

    NSArray *horizontalContraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[view2]-60-[view1(==view2)]-30-|" options:0 metrics:nil views:dict];

    NSArray *view1Height = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[view1]-30-|" options:0 metrics:nil views:dict];

    NSArray *view2Height = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[view2]-30-|" options:0 metrics:nil views:dict];

    [self.view addConstraints:horizontalContraints];
    [self.view addConstraints:view2Height];
    [self.view addConstraints:view1Height];
}

希望对您有所帮助。 :)