【发布时间】:2015-08-20 19:04:21
【问题描述】:
我正在搞一个非常简单的应用程序来学习如何使用 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];
}
【问题讨论】:
-
“水平约束似乎不起作用” “似乎不起作用”这句话毫无意义。清楚地说出你期望/想要的与正在发生的不同。
-
@matt 已编辑。如果您阅读了前几句话并稍微阅读了代码,您会看到我的 cmets 清楚地概述了我正在尝试将所有内容居中。
-
好吧,正如您已经被告知的,您的标签居中。问题是 text 不在标签中居中。但这与约束无关!
-
是的。今天是我真正看它的第一天,所以我仍然掌握它的窍门。强大的工具...顺便说一句,爱你的书@matt
-
谢谢,如果他们有帮助,我们很高兴。
标签: ios objective-c xcode autolayout programmatically-created