我正在使用可视格式语言的autoLayouts设置我的观点。
我为观点添加了一些约束。
以下是我的设置代码:
- (void)viewDidLoad
{
self.first = [UIView new];
self.second = [UIView new];
self.third = [UIView new];
self.fourth = [UIView new];
self.fifth = [UIView new];
self.first.translatesAutoresizingMaskIntoConstraints = NO;
self.second.translatesAutoresizingMaskIntoConstraints = NO;
self.third.translatesAutoresizingMaskIntoConstraints = NO;
self.fourth.translatesAutoresizingMaskIntoConstraints = NO;
self.fifth.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.first];
[self.view addSubview:self.second];
[self.view addSubview:self.third];
[self.view addSubview:self.fourth];
[self.view addSubview:self.fifth];
NSDictionary *dictionary = NSDictionaryOfVariableBindings(_first, _second, _third, _fourth, _fifth);
NSArray *constraintsArray;
NSString *format;
format = @"|[_first][_second][_third][_fourth][_fifth]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:NSLayoutFormatAlignAllLeft metrics:nil views:dictionary];
[self.view addConstraints:constraintsArray];
format = @"V:|[_first]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:NSLayoutFormatAlignAllLeft metrics:nil views:dictionary];
[self.view addConstraints:constraintsArray];
format = @"V:|[_second]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:NSLayoutFormatAlignAllLeft metrics:nil views:dictionary];
[self.view addConstraints:constraintsArray];
format = @"V:|[_third]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:NSLayoutFormatAlignAllLeft metrics:nil views:dictionary];
[self.view addConstraints:constraintsArray];
format = @"V:|[_fourth]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:NSLayoutFormatAlignAllLeft metrics:nil views:dictionary];
[self.view addConstraints:constraintsArray];
format = @"V:|[_fifth]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:NSLayoutFormatAlignAllLeft metrics:nil views:dictionary];
[self.view addConstraints:constraintsArray];
}
这就是我希望五个UIView
的行为。它们具有相同的 WIDTH和HEIGHT ,并且两个尺寸都有无边距。(第一个和最后一个)。这就是说,它们被填充在容器视图中,在这种情况下,(self.view)
当我跑步时,应用程序崩溃:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format:
Options mask required views to be aligned on a horizontal edge, which is not allowed for layout that is also horizontal.
|[_first][_second][_third][_fourth][_fifth]|
^'
我做错了什么? 请帮我。感谢。
答案 0 :(得分:4)
您应该删除对NSLayoutFormatAlignAllLeft
的所有引用。
此外,无关,但您缺少使它们具有相同宽度的约束。我用以下代码替换水平约束:
format = @"H:|[_first][_second(==_first)][_third(==_first)][_fourth(==_first)][_fifth(==_first)]|";
答案 1 :(得分:1)
格式字符串是完美的。您还可以避免所有其他垂直约束,只需添加' NSLayoutFormatAlignAllTop'而不是' NSLayoutFormatAlignAllLeft'。 基本上,当您处理与水平轴相关的格式字符串时,您必须使用与垂直轴相关的对齐选项,反之亦然(选项垂直于格式字符串),或者如果您不需要,则只需传递0。 您唯一的一种方法应如下所示:
NSString format = @"H:|[_first][_second(==_first)][_third(==_first)][_fourth(==_first)][_fifth(==_first)]|";
[NSLayoutConstraint constraintsWithVisualFormat:format
options:NSLayoutFormatAlignAllTop
metrics:nil
views:dictionary];
[编辑]
罗布是对的。 NSLayoutFormatAlignAllTop将格式字符串中所有视图的顶部对齐,而不是它们的超级视图。您必须只为一个视图添加一个垂直图钉约束,而其他所有图像也会因为&NSCHayoutFormatAlignAllTop'而对齐。另外,要删除不明确的布局,您需要添加高度约束。这是我试过的代码(高度为100点),并没有给我一个模糊的布局:NSDictionary *dictionary = NSDictionaryOfVariableBindings(_first, _second, _third, _fourth, _fifth);
NSString *format = @"H:|[_first][_second(==_first)][_third(==_first)][_fourth(==_first)][_fifth(==_first)]|";
NSMutableArray *constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format
options:NSLayoutFormatAlignAllTop
metrics:nil
views:dictionary].mutableCopy;
[constraintsArray addObject:[NSLayoutConstraint constraintWithItem:_first
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:_first.superview
attribute:NSLayoutAttributeTop
multiplier:1.0 constant:0]];
CGFloat height = 100;
for (UIView *view in @[_first,_second,_third,_fourth,_fifth]) {
[constraintsArray addObject:[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:height]];
}
[self.view addConstraints:constraintsArray];