nslayoutconstraint和auto layout无法并存?
各位大神你们好,今天我做实验,发现一旦我使用了autolayout,再在代码里面添加nslayoutconstraint就会出现如下错误:
2015-11-16 14:27:18.551 测试手动布局[9806:305517] 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.
(
"<NSLayoutConstraint:0x7fd022e1ba10 H:|-(159)-[UILabel:0x7fd022e174e0'\U6807\U7b7e'] (Names: '|':UIView:0x7fd022e1a060 )>",
"<NSLayoutConstraint:0x7fd022e19610 H:|-(100)-[UILabel:0x7fd022e174e0'\U6807\U7b7e'] (Names: '|':UIView:0x7fd022e1a060 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fd022e1ba10 H:|-(159)-[UILabel:0x7fd022e174e0'标签'] (Names: '|':UIView:0x7fd022e1a060 )>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-11-16 14:27:18.552 测试手动布局[9806:305517] 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.
(
而我在代码里面添加的约束如下所示:
_label.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint
constraintWithItem:_label
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:100].active = YES;
[NSLayoutConstraint
constraintWithItem:_label
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:100].active = YES;
[NSLayoutConstraint
constraintWithItem:_label
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:100].active = YES;
[NSLayoutConstraint
constraintWithItem:_label
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:100].active = YES;
请问,如何解决nslayoutconstraint和auto layout互相冲突的问题呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用了“autolayout”是指在 Interface Builder 中设置了约束吗? 在 IB 中设置约束跟用代码来添加 NSLayoutConstraint 是使用 autolayout 的两种方式,为什么需要同时使用呢?对同一属性添加了两个约束,数值又不一样,必然就会引起冲突的哇。