UITableViewCell中UILabel的模糊布局警告

时间:2015-02-24 12:49:58

标签: ios uitableview autolayout uilabel

我在UILabel中彼此相邻有两个UITableViewCell次观看。左边有一条线,右边有一条线,使用剩下的任何水平空间。两个标签距离单元顶部的距离相同。单元格的高度取决于右侧标签的高度。在某些情况下,我会在右侧UILabel上方和下方看到不需要的额外空间,因此它们不是顶部对齐的。再深入研究一下,我发现hasAmbiguousLayout会为两个标签返回YES

当我在调试器中调用constraintsAffectingLayoutForAxis时,我得到以下输出:

(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,我得到了这个:

(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>
)

任何人都可以根据上面的输出解释为什么这些标签的布局不明确吗?

更新: NSAutoresizingMaskLayoutConstraint约束属于单元格的contentView。两个标签都将translatesAutoresizingMaskIntoConstraints设置为NO

更新2:以下是我对contentView和两个标签的限制:

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>"
)

这张照片显示了我遇到的限制以及出了什么问题:

enter image description here

2 个答案:

答案 0 :(得分:10)

我通过添加右UILabel

来解决这个问题
[self.bodyLabel setContentHuggingPriority: UILayoutPriorityFittingSizeLevel forAxis: UILayoutConstraintAxisHorizontal];

另一件事是我在updateConstraints测试歧义,而我应该在layoutSubviews

结尾处进行测试

答案 1 :(得分:1)

啊!你关掉了吗? cell.contentView的视图层次结构中每个子视图的translatesAutoresizingMaskIntoConstraints属性?

输出显示自动布局与默认自动调整行为之间存在冲突的证据。在xib中,可以使用复选框关闭所有内容,但在代码中,您必须在每个视图级别上执行此操作。