ios - 具有不可满足约束的 UITableViewCell

标签 ios objective-c uitableview

我有一个自定义的 UITableViewCell,它的布局与我想要的完全一样,但它会引发无法满足的约束错误:

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:0x165af570 V:|-(10)-[UIButton:0x167d1bb0'Scan Patient Bracelet Squ...']   (Names: '|':UIView:0x167d1b20 )>",
"<NSLayoutConstraint:0x165af5d0 V:[UIView:0x1652b420]-(0)-|   (Names: '|':UIView:0x167d1b20 )>",
"<NSLayoutConstraint:0x165f7de0 UIView:0x1652b420.bottom == UIButton:0x167d1bb0'Scan Patient Bracelet Squ...'.bottom + 10>",
"<NSLayoutConstraint:0x165df2d0 V:[UIView:0x167d1b20]-(0)-|   (Names: '|':CMBarcodeScanCell:0x16b88800 )>",
"<NSLayoutConstraint:0x165df300 V:|-(0)-[UIView:0x167d1b20]   (Names: '|':CMBarcodeScanCell:0x16b88800 )>",
"<NSLayoutConstraint:0x167d1540 V:[CMBarcodeScanCell:0x16b88800(1)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x15fe1730 UIButton:0x15d38490'Scan Patient Bracelet Squ...'.bottom == UIView:0x15ff5750.bottom - 10>

我不知道哪个是有问题的约束。可以从上到下和从一侧到另一侧追踪约束。

有什么提示吗?

编辑

经过更多调整后,我将其归结为以下三个约束:

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:0x1478f570 UIButton:0x1475ef00'Scan Patient Bracelet Squ...'.bottom == UITableViewCellContentView:0x1476e930.bottomMargin>",
    "<NSLayoutConstraint:0x1478f5a0 UIButton:0x1475ef00'Scan Patient Bracelet Squ...'.top == UITableViewCellContentView:0x1476e930.topMargin>",
    "<NSLayoutConstraint:0x147aeb00 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x1476e930(1)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1478f570 UIButton:0x1475ef00'Scan Patient Bracelet Squ...'.bottom == UITableViewCellContentView:0x1476e930.bottomMargin>

UIView-Encapsulated-Layout-Height为1的第三个约束没有在设计中,可以看到这里:

interface builder screen shot

但现在布局不再正确。按钮文本延伸到单元格内容 View 的上方和下方。

screen shot

问题似乎是按钮内容大小不是根据标签内容大小计算的。

扫描按钮的 UIButton 调试器 View :

uibutton sizing

UIButton 文本标签的调试器 View :

uibutton label sizing

我不知道 UIButton 如何计算其内容大小,但在这种情况下它不起作用。所以现在我有 2 个问题,约束冲突和单元格布局。

最终编辑

冲突来 self 用来调整单元格大小和可变按钮文本布局的 hack。它在 heightForRowAtIndexPath:

cell.bounds = CGRectMake(0, 0, CGRectGetWidth(tTblView.bounds), 1);
cell.contentView.bounds = cell.bounds;
[cell layoutIfNeeded];

这确实使单元格布局符合要求,但引入了 contentView 高度为 1 的约束并造成了上述冲突。

我也尝试过使用 CGFLOAT_MAX 而不是 1,但这也会在运行时产生错误消息。

 cell.bounds = CGRectMake(0, 0, CGRectGetWidth(tTblView.bounds), CGFLOAT_MAX);
cell.contentView.bounds = cell.bounds;
[cell layoutIfNeeded];

所以我要关闭它并处理我的自动布局约束,直到我可以在不引入此类 hack 代码或导致约束冲突的情况下获得正确的布局。

最佳答案

The constraints can be traced from the top to the bottom and from side to side.

这里的问题纯粹是自上而下的,你可以通过研究约束很容易地看到这一点。让我们只考虑这三个约束:

V:[UIView:0x167d1b20]-(0)-| (Names: '|':CMBarcodeScanCell:0x16b88800 )
V:|-(0)-[UIView:0x167d1b20] (Names: '|':CMBarcodeScanCell:0x16b88800 )
V:[CMBarcodeScanCell:0x16b88800(1)]

所以 View 0x167 的顶部和底部被固定到 CMBarcodeScanCell 的顶部和底部 - 但随后您会遇到一个绝对高度约束,表示 CMBarcodeScanCell 仅为 1 像素高?这听起来像是一个麻烦的秘诀。

关于ios - 具有不可满足约束的 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30722463/

相关文章:

ios - 用户单击自定义按钮时,将其更改

ios - 如果存在 UIApplicationLaunchOptionsLocationKey,则启动 Cordova iOS 应用程序

ios - NSOperationQueue在所有操作完成之前已完成

swift - 如何将 fetchRequest 更改为数组以在 UITableView 中使用

ios - 如何在表格 View 中添加三个表格 View 单元格作为 subview ?

ios - 如何使用NSThread截取UIView的屏幕截图?

iphone - 按原始顺序解析/组合嵌套 HTML 元素值

ios - 从 iOS 中的字符串中删除括号

objective-c - 如何在 objective-c 中使用分数进行计算

ios - UITableViewCell 中的动画立即完成