在动态TableViewCell中添加一个按钮

时间:2015-04-20 07:01:57

标签: ios autolayout

我在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

Screenshot

我应该删除哪个约束?我不认为我添加了额外的限制

4 个答案:

答案 0 :(得分:1)

在界面构建器中检查没有警告(vc检查器侧面的黄色箭头)并检查标签+按钮和边距的高度是否超过单元格高度。

另外你说你固定在顶部,是什么?容器或标签?

答案 1 :(得分:0)

                           (OR)

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

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

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

选择视图控制器并选择编辑器&gt;解决自动布局问题&gt; []视图控制器中的所有视图&gt;从菜单中添加缺失约束: enter image description here

这将确保不会创建额外的构建时间约束,并且现在可以看到所有约束.--&gt;从视图控制器中选择所有约束: enter image description here

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

enter image description here

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

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

答案 2 :(得分:0)

你必须提供一些关于你在自动布局中设置的值的信息,比如UILabel距离上边距,下边距,左边等的距离。

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

答案 3 :(得分:0)

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

img1

img2 要么 请点击这些链接AutolayoutAutolayout programatically