暧昧布局警告中的UITableViewCell UILabels [英] Ambiguous layout warnings for UILabels in UITableViewCell

查看:424
本文介绍了暧昧布局警告中的UITableViewCell UILabels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的UITableViewCell 两个的UILabel 的意见彼此相邻。当左边有一条线,可以有多条线路并使用任何水平空间是左右。两种标记有从电池的顶部的距离相同。电池的高度由右标签的高度决定。在某些情况下,我看无用的额外空间的上方和下右的UILabel ,因此,它们不顶端对齐。展望更,我发现 hasAmbiguousLayout 收益这两个标签。

I have two UILabel views next to each other in an UITableViewCell. The left one has one line, the right one can have multiple lines and uses whatever horizontal space that is left. Both labels have the same distance from the top of the cell. The height of the cell is dictated by the height of the right label. In some cases, I see unwanted additional space above and under the right UILabel, and therefore they are not top-aligned. Looking into it more, I found that hasAmbiguousLayout returns YES for both labels.

当我称之为 constraintsAffectingLayoutForAxis 在调试器中,我得到以下的输出:

When I call constraintsAffectingLayoutForAxis in the debugger, I get the following output:

(lldb) po [0x7b769520 constraintsAffectingLayoutForAxis:0]
<__NSArrayM 0x7b6cb340>(
<NSAutoresizingMaskLayoutConstraint:0x7b76abf0 h=--& v=--& 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UITableViewCellContentView:0x7b768fc0]   (Names: '|':MyTVCell:0x7b769330'MyTVCell' )>,
<NSLayoutConstraint:0x7b76b010 H:|-(15)-[UILabel:0x7b769520'Number']   (Names: '|':UITableViewCellContentView:0x7b768fc0 )>,
<NSContentSizeLayoutConstraint:0x7b769a90 H:[UILabel:0x7b769520'Number'(131)] Hug:250 CompressionResistance:750>
)

对于第二个的UILabel 被定位成右边的第一个的UILabel ,我得到这样的:

For a second UILabel that is positioned to the right of the first UILabel, I get this:

(lldb) po [0x7b769710 constraintsAffectingLayoutForAxis:0]
<__NSArrayM 0x7b6cb9e0>(
<NSContentSizeLayoutConstraint:0x7b769a90 H:[UILabel:0x7b769520'Number'(131)] Hug:250 CompressionResistance:750>,
<NSAutoresizingMaskLayoutConstraint:0x7b76abf0 h=--& v=--& 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UITableViewCellContentView:0x7b768fc0]   (Names: '|':MyTVCell:0x7b769330'MyTVCell' )>,
<NSLayoutConstraint:0x7b76b010 H:|-(15)-[UILabel:0x7b769520'Number']   (Names: '|':UITableViewCellContentView:0x7b768fc0 )>,
<NSLayoutConstraint:0x7b76b230 H:[UILabel:0x7b769520'Number']-(15)-[UILabel:0x7b769710'Q12472']>,
<NSLayoutConstraint:0x7b76ab90 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7b768fc0(320)]>,
<NSLayoutConstraint:0x7b76b310 UILabel:0x7b769710'Q12472'.trailing == UITableViewCellContentView:0x7b768fc0.trailing - 15>
)

谁能解释一下基于上面为什么这些标签有一个模糊的布局输出?

Could anyone explain based on the output above why these labels have an ambiguous layout?

更新: NSAutoresizingMaskLayoutConstraint 约束属于内容查看细胞。这两个标签设置 translatesAutoresizingMaskIntoConstraints NO

Update: The NSAutoresizingMaskLayoutConstraint constraint belongs to the contentView of the cell. Both labels have set translatesAutoresizingMaskIntoConstraints to NO.

更新2:下面是我对内容查看的限制和两个标签:

Update 2: Here are the constraints I have for the contentView and the two labels:

2015-02-26 07:35:25.559 contentView constraints: (
    "<NSLayoutConstraint:0x7be541e0 V:|-(8)-[UILabel:0x7be537d0]   (Names: '|':UITableViewCellContentView:0x7be535a0 )>",
    "<NSLayoutConstraint:0x7be54240 H:|-(15)-[UILabel:0x7be537d0]   (Names: '|':UITableViewCellContentView:0x7be535a0 )>",
    "<NSLayoutConstraint:0x7be54310 V:|-(8)-[UILabel:0x7be53990]   (Names: '|':UITableViewCellContentView:0x7be535a0 )>",
    "<NSLayoutConstraint:0x7be54340 H:[UILabel:0x7be537d0]-(15)-[UILabel:0x7be53990]>",
    "<NSLayoutConstraint:0x7be54370 UILabel:0x7be53990.trailing == UITableViewCellContentView:0x7be535a0.trailing - 15>",
    "<NSLayoutConstraint:0x7be543c0 UILabel:0x7be53990.bottom == UITableViewCellContentView:0x7be535a0.bottom - 8>",
    "<NSContentSizeLayoutConstraint:0x7be53cd0 H:[UILabel:0x7be537d0(109)] Hug:250 CompressionResistance:750>",
    "<NSContentSizeLayoutConstraint:0x7be53d10 V:[UILabel:0x7be537d0(21)] Hug:250 CompressionResistance:750>",
    "<NSContentSizeLayoutConstraint:0x7be53eb0 H:[UILabel:0x7be53990(20)] Hug:250 CompressionResistance:750>",
    "<NSContentSizeLayoutConstraint:0x7be53f10 V:[UILabel:0x7be53990(20)] Hug:250 CompressionResistance:750>"
)
2015-02-26 07:35:25.560 left label constraints: (
    "<NSContentSizeLayoutConstraint:0x7be53cd0 H:[UILabel:0x7be537d0(109)] Hug:250 CompressionResistance:750>",
    "<NSContentSizeLayoutConstraint:0x7be53d10 V:[UILabel:0x7be537d0(21)] Hug:250 CompressionResistance:750>"
)
2015-02-26 07:35:25.561 right label constraints:(
    "<NSContentSizeLayoutConstraint:0x7be53eb0 H:[UILabel:0x7be53990(20)] Hug:250 CompressionResistance:750>",
    "<NSContentSizeLayoutConstraint:0x7be53f10 V:[UILabel:0x7be53990(20)] Hug:250 CompressionResistance:750>"
)

下面是图片显示的约束我有什么不顺心:

Here is a picture showing the constraints I have and what goes wrong:

推荐答案

我解决它通过添加合适的的UILabel

I solved it by adding for the right UILabel:

[self.bodyLabel setContentHuggingPriority: UILayoutPriorityFittingSizeLevel forAxis: UILayoutConstraintAxisHorizontal];

另一件事是,我是含糊的 updateConstraints 测试,而我应该在 layoutSubviews

这篇关于暧昧布局警告中的UITableViewCell UILabels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆