Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自动布局-确定哪些约束取代哪些约束_Ios_Objective C_Xcode_Autolayout_Programmatically Created - Fatal编程技术网

Ios 自动布局-确定哪些约束取代哪些约束

Ios 自动布局-确定哪些约束取代哪些约束,ios,objective-c,xcode,autolayout,programmatically-created,Ios,Objective C,Xcode,Autolayout,Programmatically Created,我正在摆弄一个非常简单的应用程序来学习如何使用AVFoundation(只编写了大约14周的代码) 包括一个屏幕截图,以帮助可视化我的问题-我的垂直约束工作得很好,我的水平约束显示为两个按钮,我有。但是,对于每个按钮下面的两个标签,我的水平约束(用于使一些对象居中)似乎不起作用 我想知道问题是否在于某些约束(可能是我创建它们的方式)优先于其他约束,并阻止某些约束正确出现?我真的不确定 我猜您希望这些视图居中显示。但这不是“|-[view]-|”约束的作用。您刚才告诉视图的是,“我希望您填充su

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

包括一个屏幕截图,以帮助可视化我的问题-我的垂直约束工作得很好,我的水平约束显示为两个按钮,我有。但是,对于每个按钮下面的两个标签,我的水平约束(用于使一些对象居中)似乎不起作用

我想知道问题是否在于某些约束(可能是我创建它们的方式)优先于其他约束,并阻止某些约束正确出现?我真的不确定


我猜您希望这些视图居中显示。但这不是“|-[view]-|”约束的作用。您刚才告诉视图的是,“我希望您填充superview的整个宽度,以默认填充为模”。如果要在标签上设置背景色,您会看到它们延伸视图的整个宽度


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

“水平约束似乎不起作用”短语“似乎不起作用”毫无意义。清楚地说出你期望/想要的与正在发生的事情不同的东西。@matt edited。如果你读过前面的几句话,对代码稍加了解,你会发现我的评论清楚地勾勒出我正试图将所有内容居中。好吧,正如你已经被告知的,你的标签是居中的。问题是文本没有在标签中居中。但这与约束无关!是的。今天是我第一天真正看它,所以我仍然掌握它的窍门。强大的工具…顺便说一句,爱你的书@matt谢谢,如果它们有用的话很高兴。很有意义。我试试看。我也可以只使用间距项目。希望我能在评论中发布代码,但它太混乱了。它只需要创建两个空白的UIView,其中没有任何内容,并按照如下方式执行:@“H:|[spacer1][label][spacer2(=spacer1)]|”。然而,对于我想要居中的每个项目,这似乎太多了。
-(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];

}