Ios UITableViewCell系统布局尺寸设置尺寸额外增加0.5像素

Ios UITableViewCell系统布局尺寸设置尺寸额外增加0.5像素,ios,objective-c,uitableview,autolayout,Ios,Objective C,Uitableview,Autolayout,我的单元格中只有一个按钮,我添加了高度为button=30、顶部为10、底部为10的约束条件,因此systemLayoutSizeFittingSize必须返回高度为cell=50的单元格。然而,它返回50.5,我在日志中看到: Will attempt to recover by breaking constraint 我正在使用分组表视图,并将分隔符设置为“无”。如果需要额外的0.5px 代码段: [cell layoutSubviews]; return [cell systemLa

我的单元格中只有一个按钮,我添加了高度为button=30、顶部为10、底部为10的约束条件,因此systemLayoutSizeFittingSize必须返回高度为cell=50的单元格。然而,它返回50.5,我在日志中看到:

Will attempt to recover by breaking constraint 
我正在使用分组表视图,并将分隔符设置为“无”。如果需要额外的0.5px

代码段:

[cell layoutSubviews];
return [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;// 50;
日志:

 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:0x7ffa71e273e0 V:[UIButton:0x7ffa71e24900'Book a new hotel'(30)]>",
"<NSLayoutConstraint:0x7ffa71e20bc0 V:|-(10)-[UIButton:0x7ffa71e24900'Book a new hotel']   (Names: '|':UITableViewCellContentView:0x7ffa71e06030 )>",
"<NSLayoutConstraint:0x7ffa71e23ae0 UITableViewCellContentView:0x7ffa71e06030.bottomMargin == UIButton:0x7ffa71e24900'Book a new hotel'.bottom + 2>",
"<NSLayoutConstraint:0x7ffa705f6640 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ffa71e06030(50.5)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ffa71e273e0 V:[UIButton:0x7ffa71e24900'Book a new hotel'(30)]>
无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
iOS 8

如果您的目标是iOS8,那么这会变得更容易,因为现在可以通过AutoLayout自动调整表视图单元格的大小

要执行此操作,只需在
heightForRow
方法中返回
UITableViewAutomaticDimension

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
从本质上讲,这就是我认为您正试图对代码片段执行的操作

iOS 7

对于iOS7,我会以不同的方式设置约束。我没有设置顶部和底部约束,而是设置了一个“垂直中心”约束

然后,您可以返回任何想要返回的数字,并且约束不会被打破


您没有获得自动高度功能,但高度似乎不会改变。

请显示整个错误日志。它会显示发生了什么。你的目标是iOS8吗?7+我知道有一个非常好的解决方案来解决8中的细胞高度。。但是我们需要7。我认为应该是
[cell.contentView系统布局大小设置大小:uilayoutfitting压缩大小]。高度+1你是对的,它应该是cell.contentView,而不是cell。没有必要加上“1”。我的目标是7。谢谢你告诉我这个事实,它无论如何都不会改变。但这真的很有趣,需要额外的0。5@trickster77777是的,自动布局有时会很奇怪,我自己也见过类似的事情。我猜这是因为它不能把东西放在0.5像素或其他位置。不知道为什么会这样?如果我记得很清楚,0.5是单元格分隔符的大小,并且总是添加到单元格高度。谢谢@AdrianoSantangeli