autolayout - 找出哪些约束取代哪些

autolayout - figuring out which constraints supersede which

我正在玩一个非常简单的应用程序来学习如何使用 AVFoundation(只编写了大约 14 周的代码)。

随附的屏幕截图有助于形象化我的问题 - 我的垂直约束工作得很好,我的水平约束出现在我拥有的两个按钮上。但是,我的水平约束(我用它来居中一些对象)似乎不适用于每个按钮下方的两个标签。

我想知道问题是不是某些约束(也许是我创建它们的方式)优先于其他约束并阻止某些约束正确显示?在这里真的不确定。

-(void)setConstraints {
    [self.view removeConstraints:self.view.constraints];

    UIButton *cameraButton = self.cameraButton;
    UILabel *camera = self.videoLabel;
    UIButton *libraryButton = self.libraryButton;
    UILabel *library = self.libraryLabel;

    NSDictionary *views = NSDictionaryOfVariableBindings(camera, cameraButton, libraryButton, library);

    //set up top button to be horizontally centered
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[cameraButton]-|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:views];
    //set up top button vertical from top of superview
    constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat: @"V:|-175-[cameraButton]"
                                                                                                     options:0
                                                                                                     metrics:nil
                                                                                                    views:views]];
    //set up top button label to be horizontally centered
    constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat: @"|-[camera]-|"
                                                                                                     options:0
                                                                                                     metrics:nil
                                                                                                       views:views]];

    //set up second button to be horizontally centered
    constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat: @"|-[libraryButton]-|"
                                                                                                     options:0
                                                                                                     metrics:nil
                                                                                                       views:views]];
    //set up label for second button to be horizontally centered
    constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat: @"|-[library]-|"
                                                                                                     options:0
                                                                                                     metrics:nil
                                                                                                       views:views]];

    //set up vertical constraints by spacing ALL objects appropriately
    constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat: @"V:[cameraButton]-[camera]-150-[libraryButton]-[library]"
                                                                                                     options:0
                                                                                                     metrics:nil
                                                                                                       views:views]];

    [self.view addConstraints:constraints];

}

我的猜测是您希望这些视图居中显示。但这不是“|-[视图]-|”约束会做。你刚才告诉视图的是,"I want you to fill the entire width of your superview, modulo the default padding"。如果您要在标签上设置背景颜色,您会看到它们扩展了整个视图的宽度。

此处最简单的解决方案是将文本布局设置为居中。