【问题标题】:UITableViewCell systemLayoutSizeFittingSize adds extra 0.5 pxUITableViewCell systemLayoutSizeFittingSize 额外增加 0.5 px
【发布时间】:2014-11-17 11:12:15
【问题描述】:

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

Will attempt to recover by breaking constraint 

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

代码sn-p:

[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)]>

【问题讨论】:

  • 请您显示整个错误日志。它会显示正在发生的事情。
  • 你的目标是iOS8吗?
  • 7+ 我知道 8.. 单元格高度有很好的解决方案,但我们需要 7.
  • 我觉得应该是[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1;
  • 你说得对,它应该是 cell.contentView,而不是 cell。而且不用加“1”。

标签: ios objective-c uitableview autolayout


【解决方案1】:

iOS 8

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

为此,只需在您的 heightForRow 方法中返回 UITableViewAutomaticDimension...

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

基本上这就是我认为你试图用你的代码 sn-p 做的事情。

iOS 7

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

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

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

【讨论】:

  • 我的目标是 7。感谢您指出它无论如何都不会改变的事实。但这真的很有趣,需要额外的 0.5
  • @trickster77777 是的,AutoLayout 有时会很奇怪,我自己也见过这样的事情。我想这是因为它不能将东西放置在 0.5 像素之类的地方。不知道为什么需要这样做?
  • 如果我记得 0.5 是单元格分隔符的大小,并且总是添加到单元格高度。
  • 感谢@AdrianoSantangeli
猜你喜欢
  • 2016-08-23
  • 1970-01-01
  • 2015-01-14
  • 2020-09-30
  • 1970-01-01
  • 2013-07-08
  • 2019-09-15
  • 2019-10-08
  • 1970-01-01
相关资源
最近更新 更多