nslayoutconstraint和auto layout无法并存?

发布于 2022-09-01 21:06:59 字数 2662 浏览 17 评论 0

各位大神你们好,今天我做实验,发现一旦我使用了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虫児飞 2022-09-08 21:06:59

使用了“autolayout”是指在 Interface Builder 中设置了约束吗? 在 IB 中设置约束跟用代码来添加 NSLayoutConstraint 是使用 autolayout 的两种方式,为什么需要同时使用呢?对同一属性添加了两个约束,数值又不一样,必然就会引起冲突的哇。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文