Ios 自动布局疑难约束错误

Ios 自动布局疑难约束错误,ios,objective-c,autolayout,visual-format-language,Ios,Objective C,Autolayout,Visual Format Language,我正在尝试在自定义导航栏上添加子视图。 正在使用自定义导航栏,以便增加导航栏的高度以适应额外的子视图 问题是我试图使用AutoLayout来添加新视图,当我向新视图添加VFL水平约束时,我在NSLog中得到了一个关于“无法同时满足约束”的错误,我不理解 以下是我正在做的: UIView *loadingView = [UIView new]; loadingView.translatesAutoresizingMaskIntoConstraints = NO; loadi

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

问题是我试图使用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);
这是我在日志文件中得到的eror:

    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>
我没有收到任何警告。 我无法理解为什么会发生这种情况

另外请注意,在添加我自己的约束之前,
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 )>"
)
2014-10-15 16:35:54.894斯内佩特[10984:132305]约束条件:(
"",
"",
"",
""
)
而这就是我使用``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>"
)
2014-10-15 16:37:27.140斯内佩特[11037:133374]约束条件:(
"",
"",
"",
""
)

答案是肯定的

您的CustomNavigationBar具有TranslatesAutoResizengMaskintoContraints==是。当您告诉加载视图要装入导航栏时,导航栏会调整自身大小,然后告诉加载视图其大小。这就是第二种情况成功的原因。在第一种情况下,加载视图具有高优先级高度,然后具有一些默认优先级所有与TranslatesAutoResizengMaskinToConstraints指定的默认优先级垂直间距冲突的字符串

尝试:


这不起作用,因为它会使应用程序崩溃:
***由于未捕获的异常“nsinternalinconsistenceexception”终止应用程序,原因:“无法修改控制器管理的UINavigationBar的约束”
更令人费解的是,如果我逐个添加约束,而不是从
NSMutabl中立即添加约束,我会收到一组不同的约束错误耳环
(请参见我的更新问题)好吧,我认为你的问题的答案是,你不能对作为UINavigationController一部分显示的UINavigationBar做你想做的事情。你可能需要重新考虑你的设计,以拥有标题视图,或者对于包含在UINavigationController中的特定视图控制器,隐藏UINavigationControllerr UINavigationBar,并让您的控制器添加并控制自己的UINavigationBar。导致您出现问题的真正问题是,您不拥有或控制的固定高度UINavigationBar内部加载视图的高度限制:V:|-38-[loadingView](30@1000)]此约束表示超级视图需要为标准垂直空间+38+标准项目间垂直空间+30高,但UINavigationController约束了导航栏,不允许其增长那么高。问题是由水平约束而非垂直约束引起的。是的,如果我无法用AutoLayout解决此问题,我将我使用旧的springs&sprouts布局并使用它。
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>"
)
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];