我正在尝试在自定义导航栏上添加子视图。正在使用自定义导航栏,以便增加栏高度以适应额外的子视图。
问题是我正在尝试使用 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>"
)