UITableViewCell systemLayoutSizeFittingSize增加了额外的0.5像素

时间:2014-11-17 11:12:16

标签: ios objective-c uitableview autolayout

我的单元格中只有一个按钮,我添加了按钮的高度= 30,顶部= 10和底部= 10个约束,因此systemLayoutSizeFittingSize必须返回单元格的高度= 50.然而它返回50.5并且我看到了日志:

Will attempt to recover by breaking constraint 

我正在使用分组表视图和分隔符设置为none。需要额外0.5px的地方?

constraints

代码段:

[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 个答案:

答案 0 :(得分:3)

iOS 8

如果您的目标是iOS8,那么现在可以通过AutoLayout自动调整表格视图单元格,从而使这更加容易。

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

基本上,这就是我认为你试图用你的代码片段做的事情。

iOS 7

对于iOS 7,我会以不同的方式设置约束。我没有设置顶部和底部约束,而只是“垂直居中”约束。

然后你可以返回你想要返回的任何数字,但约束不会中断。

你没有获得自动高度的东西,但高度似乎无论如何都没有改变。