使用约束以编程方式布局多个视图

时间:2015-11-10 09:04:14

标签: ios objective-c autolayout

如何使用objective-c中的约束以编程方式创建2 x 2 UIViews网格?

例如,我有四个UIViews,其名称对应于它们的行/列位置(oneOne,oneTwo,twoOne,twoTwo),如下图所示。

layout using constraints

以下是我目前使用的代码:

-(void)buildGrid {

    [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];

    UIView *oneOne = [UIView new];
    [oneOne setBackgroundColor:[UIColor redColor]];
    [oneOne setTranslatesAutoresizingMaskIntoConstraints:NO];

    UIView *oneTwo = [UIView new];
    [oneTwo setBackgroundColor:[UIColor greenColor]];
    [oneTwo setTranslatesAutoresizingMaskIntoConstraints:NO];

    UIView *twoOne = [UIView new];
    [twoOne setBackgroundColor:[UIColor blueColor]];
    [twoOne setTranslatesAutoresizingMaskIntoConstraints:NO];

    UIView *twoTwo = [UIView new];
    [twoTwo setBackgroundColor:[UIColor purpleColor]];
    [twoTwo setTranslatesAutoresizingMaskIntoConstraints:NO];

    [self.view addSubview:oneOne];
    [self.view addSubview:oneTwo];
    [self.view addSubview:twoOne];
    [self.view addSubview:twoTwo];

    NSDictionary *viewsDict = NSDictionaryOfVariableBindings(oneOne, oneTwo, twoOne, twoTwo);



    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[oneOne]-10-[twoOne]-10-|"
                                                                  options:NSLayoutFormatDirectionLeadingToTrailing
                                                                  metrics:nil
                                                                    views:viewsDict]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[oneOne]-10-[twoOne]-10-|"
                                                                  options:NSLayoutFormatDirectionLeadingToTrailing
                                                                  metrics:nil
                                                                    views:viewsDict]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[oneTwo]-10-[twoTwo]-10-|"
                                                                  options:NSLayoutFormatDirectionLeadingToTrailing
                                                                  metrics:nil
                                                                    views:viewsDict]];


    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[twoOne]-10-[twoTwo]-10-|"
                                                                  options:NSLayoutFormatDirectionLeadingToTrailing
                                                                  metrics:nil
                                                                    views:viewsDict]];

}

我已经按照了几个教程,但没有(到目前为止)已经描述了如何创建这种'网格'布局,它们就像一个单一的列一样简单。 UIViews,但当我有更多的专栏'我得到的错误是无法维持约束:

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.

如果我将四个UIViews关闭或者在屏幕上移动,这个代码可以正常工作,但如果我将其设为2 x 2则不行。但我做错了什么?

更新

根据接受的答案,限制应该是:

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[oneOne]-10-[twoOne(==oneOne)]-10-|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:viewsDict]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[oneTwo]-10-[twoTwo(==oneTwo)]-10-|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:viewsDict]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[oneOne]-10-[oneTwo(==oneOne)]-10-|"
                                                                  options:NSLayoutFormatDirectionLeadingToTrailing
                                                                  metrics:nil
                                                                    views:viewsDict]];


    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[twoOne]-10-[twoTwo(==twoOne)]-10-|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:viewsDict]];

1 个答案:

答案 0 :(得分:1)

你们中的第一个在第二个约束中有错误。它应该是:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[oneOne]-10-[oneTwo]-10-|"
                                                                  options:NSLayoutFormatDirectionLeadingToTrailing
                                                                  metrics:nil
                                                                    views:viewsDict]];

此外,您的布局仍然不包含有关子视图大小(宽度和高度)的任何信息。你必须添加这样的约束(在伪代码中)

 oneOne.width = oneTwo.width = twoOne.width = twoTwo.width
 oneOne.height = oneTwo.height = twoOne.height = twoTwo.height