无法同时满足约束。虽然没有定义重复/不需要的约束

Posted

技术标签:

【中文标题】无法同时满足约束。虽然没有定义重复/不需要的约束【英文标题】:Unable to simultaneously satisfy constraints. Although no duplicate/unwanted constraint is defined 【发布时间】:2018-01-17 07:22:54 【问题描述】:

标题听起来可能重复,但我已经尝试解决了几个小时,但找不到任何重复或不需要的约束。

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:0x608000491b70 UILabel:0x7fcb77544bb0'Order ID: '.centerY == UIView:0x7fcb77511c30.centerY   (active)>",
    "<NSLayoutConstraint:0x6080004922f0 V:[UILabel:0x7fcb7753fb20'12-Oct-2017, 5:51 PM']-(8)-|   (active, names: '|':UIView:0x7fcb77547090 )>",
    "<NSLayoutConstraint:0x608000492340 V:[UILabel:0x7fcb77544bb0'Order ID: ']-(8)-[UILabel:0x7fcb7753fb20'12-Oct-2017, 5:51 PM']   (active)>",
    "<NSLayoutConstraint:0x608000492200 V:|-(8)-[UIView:0x7fcb77511c30]   (active, names: '|':UIView:0x7fcb77547090 )>",
    "<NSLayoutConstraint:0x608000492de0 V:|-(0)-[UIView:0x7fcb77549a00]   (active, names: '|':UIView:0x7fcb7753e4b0 )>",
    "<NSLayoutConstraint:0x608000492f20 UILabel:0x7fcb77549e30'Zxcvb'.top == UIView:0x7fcb77549a00.top + 8   (active)>",
    "<NSLayoutConstraint:0x608000492fc0 V:[UILabel:0x7fcb77549e30'Zxcvb']-(8)-[UILabel:0x7fcb7754a0c0'Address, 16...']   (active)>",
    "<NSLayoutConstraint:0x6080004930b0 V:[UILabel:0x7fcb7754a0c0'Address, 16...']-(8)-|   (active, names: '|':UIView:0x7fcb7753e4b0 )>",
    "<NSLayoutConstraint:0x6080004934c0 V:|-(8)-[UILabel:0x7fcb7754a780]   (active, names: '|':UIView:0x7fcb7754a350 )>",
    "<NSLayoutConstraint:0x608000493510 V:[UILabel:0x7fcb7754a780]-(8)-|   (active, names: '|':UIView:0x7fcb7754a350 )>",
    "<NSLayoutConstraint:0x608000493740 V:|-(0)-[UIView:0x7fcb77547090]   (active, names: '|':UIView:0x7fcb77547750 )>",
    "<NSLayoutConstraint:0x608000493880 V:[UIView:0x7fcb77547090]-(0)-[UIView:0x7fcb77548c30]   (active)>",
    "<NSLayoutConstraint:0x6080004938d0 V:[UIView:0x7fcb77548c30]-(0)-[UIView:0x7fcb7753e4b0]   (active)>",
    "<NSLayoutConstraint:0x608000493a60 V:[UIView:0x7fcb7754a350]-(0)-|   (active, names: '|':UIView:0x7fcb77547750 )>",
    "<NSLayoutConstraint:0x608000493ab0 V:[UIView:0x7fcb7753e4b0]-(0)-[UIView:0x7fcb7754a350]   (active)>",
    "<NSLayoutConstraint:0x608000493b50 V:|-(4)-[UIView:0x7fcb77547750]   (active, names: '|':UITableViewCellContentView:0x7fcb77523ff0 )>",
    "<NSLayoutConstraint:0x608000493bf0 V:[UIView:0x7fcb77547750]-(4)-|   (active, names: '|':UITableViewCellContentView:0x7fcb77523ff0 )>",
    "<NSLayoutConstraint:0x600000680a50 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fcb77523ff0.height == 68   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x608000492fc0 V:[UILabel:0x7fcb77549e30'Zxcvb']-(8)-[UILabel:0x7fcb7754a0c0'Address, 16...']   (active)>

产生警告的代码部分是:

"<NSLayoutConstraint:0x608000492f20 UILabel:0x7fcb77549e30'Zxcvb'.top == UIView:0x7fcb77549a00.top + 8   (active)>",
"<NSLayoutConstraint:0x608000492fc0 V:[UILabel:0x7fcb77549e30'Zxcvb']-(8)-[UILabel:0x7fcb7754a0c0'Address, 16...']   (active)>",
"<NSLayoutConstraint:0x6080004930b0 V:[UILabel:0x7fcb7754a0c0'Address, 16...']-(8)-|   (active, names: '|':UIView:0x7fcb7753e4b0 )>",

但是所有这 3 个约束都需要满足一般要求。

--------[View starts]-----------
---- 8 point space to top-------
[UILabel:0x7fcb77549e30'Zxcvb']
---- 8 point space in between---
[UILabel:0x7fcb7754a0c0'Address, 16...']
---- 8 point space to bottom----
---------[View Ends]------------

但以下约束正在被打破以恢复:

[UILabel:0x7fcb77549e30'Zxcvb']
---- 8 point space in between---
[UILabel:0x7fcb7754a0c0'Address, 16...']

但是如果没有这个约束,视图就无法生存。

所有这些都发生在 tableview 单元格内,并且随机发生。 我还为上述约束定义了IBOutlet,并且在某些条件下将其更改为08

表格视图单元格是通过 XIB 创建的,因此排除了 translatesAutoresizingMaskIntoConstraints = false

【问题讨论】:

看起来你的单元格高度是 68,但是你单元格中所有项目的约束要求它高于 68 我没有将高度设置为68,表格单元格高度为UITableViewAutomaticDimension wtfautolayout 另有说明 我认为 "&lt;NSLayoutConstraint:0x600000680a50 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fcb77523ff0.height == 68 (active)&gt;" ,它是自动生成的。和 wtfautolayout 说† This constraint was added by a table or collection view to enforce its cell size. 你告诉 Autolayout 那个约束不太重要,所以它可以在没有警告的情况下打破这个约束。 【参考方案1】:

将单元格中最底部项的“底部约束”的“优先级”设置为750,以免与UIView-Encapsulated-Layout-Height冲突。

【讨论】:

这是一个规则还是背后有一些普遍的想法?因为我在过去找不到任何做这件事的感觉。 :// 抱歉没有,因为单元格布局在加载时会自动向单元格添加高度约束,这可能不符合单元格的内部约束并且具有 1000 优先级,因此必须打破约束,但它不会损害结构,因为它最终会通过单元格出队找出正确的约束,如果你滚动你将不再看到冲突 我会选择Priority 999

以上是关于无法同时满足约束。虽然没有定义重复/不需要的约束的主要内容,如果未能解决你的问题,请参考以下文章

自动布局无法同时满足约束[重复]

关闭:“无法同时满足约束”[重复]

UIAlertController(ActionSheet)-无法同时满足约束[重复]

iOS 8 自定义键盘:更改高度而不发出警告“无法同时满足约束...”

iOS:无法同时满足约束

以编程方式应用自动布局约束时无法同时满足约束