设置视觉格式语言时 iAutoLayout 崩溃

iAutoLayout crash when setting Visual Format Language

嗨,我正在使用具有视觉格式语言的自动布局设置我的视图。

我对我的观点进行了一些限制。

以下是我的代码设置方式:

- (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]| 
                  ^'

我做错了什么? 请帮我。谢谢

您应该删除所有对 NSLayoutFormatAlignAllLeft 的引用。

此外,不相关,但您缺少使它们具有相同宽度的约束。我将水平约束替换为:

format = @"H:|[_first][_second(==_first)][_third(==_first)][_fourth(==_first)][_fifth(==_first)]|";

Rob 提出的格式字符串很完美。您还可以避免所有其他垂直约束,只需添加 'NSLayoutFormatAlignAllTop' 而不是 'NSLayoutFormatAlignAllLeft'。 基本上,当您处理与水平轴相关的格式字符串时,您必须使用与垂直轴相关的对齐选项,反之亦然(选项垂直于格式字符串),或者如果您不需要它,只需传递 0。 您唯一的一种方法应该如下所示:

NSString format = @"H:|[_first][_second(==_first)][_third(==_first)][_fourth(==_first)][_fifth(==_first)]|";

[NSLayoutConstraint constraintsWithVisualFormat:format 
                                        options:NSLayoutFormatAlignAllTop
                                        metrics:nil
                                          views:dictionary];

[编辑]

Rob 是对的。 NSLayoutFormatAlignAllTop 在它们之间的格式字符串内对齐视图的所有顶部,而不是它们的父视图。由于 'NSLayoutFormatAlignAllTop',您必须仅向其中一个视图添加一个垂直引脚约束,而所有其他视图也将对齐。此外,要删除不明确的布局,您需要添加高度约束。这是我试过的代码(高度为 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];