AutoLayout令人困惑的约束错误

时间:2014-10-15 13:07:08

标签: ios objective-c autolayout visual-format-language

我正在尝试在自定义导航栏上添加子视图。 正在使用自定义导航栏,以便增加条形高度以适应额外的子视图。

问题是我正在尝试使用AutoLayout添加新视图,当我向我的新视图添加VFL水平约束时,我在NSLog中收到关于“无法同时满足约束”的错误,我不知道不明白。

以下是我正在做的事情:

    UIView *loadingView = [UIView new];
    loadingView.translatesAutoresizingMaskIntoConstraints = NO;
    loadingView.backgroundColor = [UIColor redColor];
    [self addSubview:loadingView];


    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-38-[loadingView(30@1000)]"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:NSDictionaryOfVariableBindings(loadingView)]];

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[loadingView]-|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:NSDictionaryOfVariableBindings(loadingView)]];

    NSLog(@"CONSTRAINTS:%@",self.constraints);

这就是我在日志文件中的错误:

    Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7ad422d0 UIView:0x7ad412c0.leading == CustomNavigationBar:0x7ad388b0.leadingMargin>",
    "<NSLayoutConstraint:0x7ad14a30 CustomNavigationBar:0x7ad388b0.trailingMargin == UIView:0x7ad412c0.trailing>",
    "<NSLayoutConstraint:0x79e8d990 H:[CustomNavigationBar:0x7ad388b0(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ad14a30 CustomNavigationBar:0x7ad388b0.trailingMargin == UIView:0x7ad412c0.trailing>

如果我从我的VFL中移除标准间距,意味着我使用:

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[loadingView]|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:NSDictionaryOfVariableBindings(loadingView)]];

我没有收到警告。 我不明白为什么会这样。

另请注意,UINavigationBar约束(self.constraints)在添加自己的约束之前是空的,因此这不会导致任何冲突的约束。

在这两种情况下我也在UINavigationBar self.constraints上做了一个NSLog,这是案例1中的NSLog(使用VFL H:|[loadingView]|时没有错误:

2014-10-15 16:35:54.894 Snapette[10984:132305] CONSTRAINTS:(
    "<NSLayoutConstraint:0x7a938900 V:|-(38)-[UIView:0x7a996290]   (Names: '|':CustomNavigationBar:0x7a98d6b0 )>",
    "<NSLayoutConstraint:0x7a93a740 V:[UIView:0x7a996290(30)]>",
    "<NSLayoutConstraint:0x7a997260 H:|-(0)-[UIView:0x7a996290]   (Names: '|':CustomNavigationBar:0x7a98d6b0 )>",
    "<NSLayoutConstraint:0x7a93b7f0 H:[UIView:0x7a996290]-(0)-|   (Names: '|':CustomNavigationBar:0x7a98d6b0 )>"
)

虽然这是我使用``H:| - [loadingView] - |`

时得到的
2014-10-15 16:37:27.140 Snapette[11037:133374] CONSTRAINTS:(
    "<NSLayoutConstraint:0x7ad102a0 V:|-(38)-[UIView:0x7ad412c0]   (Names: '|':CustomNavigationBar:0x7ad388b0 )>",
    "<NSLayoutConstraint:0x7ad15690 V:[UIView:0x7ad412c0(30)]>",
    "<NSLayoutConstraint:0x7ad422d0 UIView:0x7ad412c0.leading == CustomNavigationBar:0x7ad388b0.leadingMargin>",
    "<NSLayoutConstraint:0x7ad14a30 CustomNavigationBar:0x7ad388b0.trailingMargin == UIView:0x7ad412c0.trailing>"
)

1 个答案:

答案 0 :(得分:0)

"<NSAutoresizingMaskLayoutConstraint:0x7ac748d0 h=-&- v=--& V:[CustomNavigationBar:0x7d96ed50(76)]>"

有答案。

您的CustomNavigationBar已翻译了AutoresizingMaskIntoContraints == YES。当您告诉加载视图适合导航栏时,导航栏会自行调整大小,然后告诉loadingView它的大小。这就是你的第二个案例成功的原因。在第一种情况下,加载视图具有高优先级高度,然后是一些默认优先级垂直间距,这些都与translatesAutoresizingMaskIntoConstraints指定的默认优先级垂直间距冲突。

尝试:

self.translatesAutoresizingMaskIntoConstraints = NO;

UIView *loadingView = [UIView new];
loadingView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:loadingView];

NSMutableArray *constraints = [NSMutableArray new];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[loadingView]-|"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:NSDictionaryOfVariableBindings(loadingView)]];

[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-38-[loadingView(30@1000)]|"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:NSDictionaryOfVariableBindings(loadingView)]];

[self addConstraints:constraints];
loadingView.backgroundColor = [UIColor redColor];