尝试加载 xib 文件时无法同时满足约束

Posted

技术标签:

【中文标题】尝试加载 xib 文件时无法同时满足约束【英文标题】:Unable to simultaneously satisfy constraints When trying to load xib file 【发布时间】:2016-12-14 11:50:53 【问题描述】:

我正在尝试使用 xib 文件实现 UITableView 但是,当我运行该应用程序时,我收到以下错误,并且 xib 在 iPhone4s 等小屏幕中正确显示 它适用于较大的显示尺寸。

Screenshot of xib

任何帮助将不胜感激

错误

    2016-12-14 17:12:41.826 FoodStrock[5776:1761262] 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:0x7fcc2a6dfac0 V:[UIImageView:0x7fcc2a6e1010(63)]>",
    "<NSLayoutConstraint:0x7fcc2a617530 V:[UIImageView:0x7fcc2a6e1010]-(8)-|   (Names: '|':UITableViewCellContentView:0x7fcc2a6e1270 )>",
    "<NSLayoutConstraint:0x7fcc2a6cc3b0 V:|-(9)-[UIImageView:0x7fcc2a6e1010]   (Names: '|':UITableViewCellContentView:0x7fcc2a6e1270 )>",
    "<NSLayoutConstraint:0x7fcc2a4955a0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fcc2a6e1270(59.6667)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fcc2a6dfac0 V:[UIImageView:0x7fcc2a6e1010(63)]>

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.
2016-12-14 17:12:41.827 FoodStrock[5776:1761262] 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:0x7fcc2a6a69d0 V:[UILabel:0x7fcc2a6e1670'Address'(63)]>",
    "<NSLayoutConstraint:0x7fcc2a693820 UILabel:0x7fcc2a6e1670'Address'.bottom == UITableViewCellContentView:0x7fcc2a6e1270.bottomMargin>",
    "<NSLayoutConstraint:0x7fcc2a6e0c00 UILabel:0x7fcc2a6e1670'Address'.top == UITableViewCellContentView:0x7fcc2a6e1270.topMargin + 1>",
    "<NSLayoutConstraint:0x7fcc2a4955a0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fcc2a6e1270(59.6667)]>"
)

【问题讨论】:

正确检查您的约束。该错误是由于额外的限制而发生的。检查每个约束并删除不必要的约束。 【参考方案1】:

将你的 UIImageView 、 UILabel 、 UIButton 放在单个 UIView 中(比如 mainView)。现在 mainView 约束将是 xib 视图(superView)的前导、尾随、顶部底部。现在 UIImageView , UILabel , UIButton 的约束是根据您的要求 w.r.t.主视图。

注意:尽可能减少对 UIComponents 的约束,但请注意它完全满足您的要求。像这里一样,给出这样的约束:

    UIImage : top, bottom , Leading wrt mainView and constant width 或者你可以给容器水平居中,前导,高度,宽度。

    UIButton : top, bottom, trailing wrt mainView 和固定宽度,或者你可以给容器水平居中,前导,高度,宽度。

    UILabel : 前导尾随和水平居中到容器就足够了,但你也可以给出高度。 UILabel 可以根据你只需要指定x和y坐标的文字字体大小自动取高宽。

【讨论】:

感谢 Tinu 的帮助,但我在 xib 文件中找不到 UIView 老兄,你必须添加新的 UIView,在故事板的右下方,拖放它。然后将所有组件放入其中,然后使用它。

以上是关于尝试加载 xib 文件时无法同时满足约束的主要内容,如果未能解决你的问题,请参考以下文章

无法同时满足约束。可能以下列表中的至少一个约束是您不想要的

无法同时满足约束

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

UIAlertview 错误:无法同时满足约束

iOS - AutoLayout'无法同时满足约束'

自动调整 UITableViewCell 大小:无法同时满足约束