[英]AutoLayout puzzling constraints error
我正在尝试在自定义导航栏上添加子视图。 正在使用“自定义导航栏”,以便增加栏的高度以适合额外的子视图。
问题是我试图使用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>"
)
"<NSAutoresizingMaskLayoutConstraint:0x7ac748d0 h=-&- v=--& V:[CustomNavigationBar:0x7d96ed50(76)]>"
有答案。
您的CustomNavigationBar具有translationsAutoresizingMaskIntoContraints ==是。 当您告诉加载视图适合导航栏时,导航栏会自动调整大小,然后告诉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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.