UITableViewCell systemLayoutSizeFittingSize 额外增加 0.5 px

tri*_*777 6 objective-c uitableview ios autolayout

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

Will attempt to recover by breaking constraint 
Run Code Online (Sandbox Code Playgroud)

我正在使用分组表视图并将分隔符设置为无。哪里需要额外0.5px?

限制条件

代码片段:

[cell layoutSubviews];
return [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;// 50;
Run Code Online (Sandbox Code Playgroud)

日志:

 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)]>
Run Code Online (Sandbox Code Playgroud)

Fog*_*ter 3

iOS 8

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

要做到这一点,只需返回UITableViewAutomaticDimension你的heightForRow方法......

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
Run Code Online (Sandbox Code Playgroud)

本质上,这就是我认为您正在尝试对代码片段执行的操作。

IOS 7

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

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

你没有得到自动高度的东西,但高度似乎并没有改变。

  • 如果我没记错的话,0.5 是单元格分隔符的大小,并且总是添加到单元格高度中。 (3认同)