autolayout - 确定哪些约束取代哪个

时间:2015-08-20 19:04:21

标签: ios objective-c xcode autolayout programmatically-created

我正在使用一个非常简单的应用程序来学习如何使用AVFoundation(仅在14周内完成编码)。

包含一个帮助可视化我的问题的屏幕截图 - 我的垂直约束工作正常,我的水平约束出现在我拥有的两个按钮上。但是,我的水平约束(我用来居中几个对象)似乎不适用于每个按钮下面的两个标签。

我想知道问题是否有些限制(可能是我创造它们的方式)优先于其他限制并防止某些约束正确显示?真的不确定。

enter image description here

-(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];

}

1 个答案:

答案 0 :(得分:0)

我猜您希望这些视图显示为居中。但那并不是什么" | - [查看] - |"约束会做。你刚刚告诉视图的是,"我希望你填充你的超级视图的整个宽度,模数默认的填充"。如果您要在标签上设置背景颜色,则会看到它们会延伸视图的整个宽度。

这里最简单的解决方案是将文本布局设置为中心。