Ios 在动态TableViewCell中添加按钮

Ios 在动态TableViewCell中添加按钮,ios,autolayout,Ios,Autolayout,我在TableViewCell中有一个动态大小的标签。我已将其固定在上、下、左、右,并将线条设置为0,它工作正常。接下来,我在标签下方添加一个按钮,并将其固定在顶部和左侧。并固定宽度和高度。但我在运行时遇到了一个错误 Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try th

我在TableViewCell中有一个动态大小的标签。我已将其固定在上、下、左、右,并将线条设置为0,它工作正常。接下来,我在标签下方添加一个按钮,并将其固定在顶部和左侧。并固定宽度和高度。但我在运行时遇到了一个错误

 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:0x7fcbc302f600 UILabel:0x7fcbc3034010'Digestion of food  in the...'.top == UITableViewCellContentView:0x7fcbc30334a0.topMargin>",
"<NSLayoutConstraint:0x7fcbc3037e30 UITableViewCellContentView:0x7fcbc30334a0.bottomMargin == UILabel:0x7fcbc3034010'Digestion of food  in the...'.bottom + 38>",
   "<NSLayoutConstraint:0x7fcbc303a920 'UIView-Encapsulated-Layout-Height' V: [UITableViewCellContentView:0x7fcbc30334a0(43.5)]>"
 )

 Will attempt to recover by breaking constraint 
 <NSLayoutConstraint:0x7fcbc3037e30 
 UITableViewCellContentView:0x7fcbc30334a0.bottomMargin ==
 UILabel:0x7fcbc3034010'Digestion of food  in the...'.bottom + 38>

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. Here is the screenshot of my layout
无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
尝试以下方法:(1)查看每个约束,并尝试找出您需要的约束
不要期待;(2) 查找添加不需要的约束的代码或
约束并修复它。(注意:如果您看到
如果您不了解NSAutoresizingMaskLayoutConstraints,请参阅
UIView属性translatesAutoresizingMaskIntoConstraints的文档
(
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。这是我的布局截图


我应该删除哪个约束?我认为我没有在interface builder中添加额外的约束,请检查是否没有警告(vc inspector侧面的黄色箭头),并检查标签+按钮和边距的高度是否不超过单元格高度

你还说你被钉在上面,什么?容器还是标签

                           (OR)
出于某种原因,Xcode在构建时在NIB上生成自己的默认自动布局约束集。这就是为什么我不能再添加任何手动约束,因为它们与自动添加的约束冲突

我通过以下方式解决了这个问题:

打开正在处理的情节提要视图控制器

选择视图控制器,然后从菜单中选择编辑器>解决自动布局问题>所有视图[]视图控制器>添加缺少的约束:

这将确保不会创建其他生成时约束,并且所有约束现在都可见。-->从视图控制器中选择所有约束:

这将确保不会创建其他生成时约束,并且所有约束现在都可见。-->从右窗格中选中以下复选框:占位符-在生成时删除:

(这将确保不会创建其他构建时约束,并且所有约束现在都可见。)


现在,您可以在代码中手动添加所有自动布局约束。

您必须提供更多有关在自动布局中设置的值的信息,如UILabel与上边距、下边距、左边距等的距离


问题可能是您将UIButton指定为从上页边距开始,它应该相对于UILabel,因为它位于UILabel下方,因此将UIButton的垂直间距属性设置为UILabel,以确保UIButton位于其下方。

根据您的要求使用这些约束:


遵循这些链接或

单元格高度随标签大小而变化。我只希望按钮显示在标签文本下方。固定在标签顶部按钮顶部应固定在标签底部。我想这是一个定制单元,对吧?你能显示你的布局屏幕吗?请确保标签高度不超过单元格的高度。请检查一下布局好吗?尝试将标签行设置为1。它可能不会有任何问题。然后,如果需要显示更多行,则可能需要进一步调整不起作用的内容(减小文本大小或增加单元格高度或减少边距)。只有按钮显示在第一行,而不是文本。是的,但这通常意味着AutoLayout的使用不好,甚至更糟糕,因为它明确地告诉您存在布局问题(分辨率标记)