本文介绍了的UITableViewCell systemLayoutSizeFittingSize增加了额外的0.5像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾与中只有一个纽扣电池,我已经添加按钮= 30的高度,顶部= 10和底部= 10的限制,所以systemLayoutSizeFittingSize必须返回细胞的高度= 50。尽管如此,它返回50.5和我在看日志:

 将试图通过打破约束恢复

我用设置为none分组表视图和分隔符。它需要额外的0.5px?

code片断:

  [单元layoutSubviews]
返回[单元systemLayoutSizeFittingSize:UILayoutFittingCom pressedSize] .height; // 50;

日志:

 无法同时满足约束条件。
大概在以下列表中的约束条件中的至少一个是一个你不想要的。试试这个:(1)看一下每个约束和揣摩,你不要指望; (2)找到code,它增加了不必要的约束或限制并修复它。 (注:如果你看到你不明白NSAutoresizingMaskLayoutConstraints,请参阅文档UIView的财产translatesAutoresizingMaskIntoConstraints)

< NSLayoutConstraint:0x7ffa71e273e0五:[的UIButton:0x7ffa71e24900'Book一个新的酒店(30)]>中,
< NSLayoutConstraint:0x7ffa71e20bc0五:| - (10) - 的UIButton:0x7ffa71e24900'Book一个新的酒店(名称:'|':UITableViewCellContentView:0x7ffa71e06030)>中,
&所述; NSLayoutConstraint:0x7ffa71e23ae0 UITableViewCellContentView:0x7ffa71e06030.bottomMargin ==的UIButton:0x7ffa71e24900'Book新hotel'.bottom +2;,
< NSLayoutConstraint:0x7ffa705f6640'的UIView封装 - 布局 - 身高V:[UITableViewCellContentView:0x7ffa71e06030(50.5)]≥
)将试图打破约束恢复
< NSLayoutConstraint:0x7ffa71e273e0五:[的UIButton:0x7ffa71e24900'Book一个新的酒店(30)]>


解决方案

的iOS 8

如果你的目标是iOS8上那么它使这更简单的表格视图单元格,现在可以自动地通过自动版式大小。

要做到这一点刚刚返回 UITableViewAutomaticDimension heightForRow 方法...

   - (CGFloat的)的tableView:(UITableView的*)的tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    返回UITableViewAutomaticDimension;
}

这基本上做什么,我想你想你的code片段做的。

的iOS 7

有关iOS的7我想不同的设置了限制。而不是设置在顶部和底部的约束我只希望有一个中心垂直的约束。

然后就可以回到你想要返回任何数量和约束不会打破。

您没有得到自动高度的事情,但高度不会出现无论如何改变。

I have cell with only one button in it, I've added height of button = 30, top = 10 and bottom = 10 constraints, so systemLayoutSizeFittingSize must return height of cell = 50. Nevertheless it returns 50.5 and I see in the log :

Will attempt to recover by breaking constraint

I'm using grouped table view and separator set to none. Where it takes extra 0.5px?

code snippet:

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

log:

 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)]>
解决方案

iOS 8

If your target is iOS8 then it makes this much easier as table view cells can now be sized automagically by AutoLayout.

To do this just return UITableViewAutomaticDimension in your heightForRow method...

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

Essentially this does what I think you are trying to do with your code snippet.

iOS 7

For iOS 7 I'd set out the constraints differently. Instead of setting the top and bottom constraints I'd just have a "centre vertically" constraint.

Then you can return any number you want to return and the constraints won't break.

You don't get the automatic height thing but the height doesn't appear to change anyway.

这篇关于的UITableViewCell systemLayoutSizeFittingSize增加了额外的0.5像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:21