簡體   English   中英

設置視覺格式語言時,iAutoLayout崩潰

[英]iAutoLayout crash when setting Visual Format Language

嗨,我正在使用帶有視覺格式語言的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]| 
                  ^'

我做錯了什么? 請幫我。 謝謝。

您應該刪除所有對NSLayoutFormatAlignAllLeft引用。

同樣,它們不相關,但是您缺少使它們具有相同寬度的約束。 我將水平約束替換為:

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

Rob提出的格式字符串是完美的。 您也可以僅添加“ NSLayoutFormatAlignAllAll”而不是“ NSLayoutFormatAlignAllLeft”來避免所有其他垂直約束。 基本上,當您處理與水平軸相關的格式字符串時,必須使用與垂直軸相關的對齊選項,反之亦然(該選項垂直於格式字符串),或者如果不需要,則僅傳遞0。唯一的一種方法應該看起來像這樣:

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

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

[編輯]

羅伯是對的。 NSLayoutFormatAlignAllTop會將視圖的所有上側在它們之間的格式字符串中對齊,而不是與其上級視圖對齊。 您只需要向一個視圖添加一個垂直的釘子約束,並且由於“ NSLayoutFormatAlignAllAllTop”,所有其他視圖也將對齊。 另外,要消除歧義的布局,您還需要添加高度限制。 這是我嘗試的代碼(高度為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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM