在动态 TableViewCell 中添加一个按钮

Adding a button in dynamic TableViewCell

我在 TableViewCell 中有一个动态大小的标签。我已将它固定到上、下、左和右,并将 Lines 设置为 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

我应该删除哪个约束?我认为我没有添加额外的约束

在 Interface Builder 中检查没有警告(vc 检查器一侧的黄色箭头)并检查标签 + 按钮的高度和边距不超过单元格高度。

你还说你置顶了,什么?容器还是标签?

                           (OR)

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

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

打开您正在处理的 Storyboard 视图控制器。

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

这将确保不会创建额外的构建时间约束,并且所有约束现在都可见。-->Select 来自您的视图控制器的所有约束:

这将确保不会创建额外的构建时间约束并且所有约束现在都可见。-->从右侧窗格中选中以下复选框:占位符 - 在构建时删除:

(这将确保不会创建额外的构建时间约束并且所有约束现在都可见。)

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

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

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

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

或者 点击这些链接 Autolayout or Autolayout programatically