我在UITableViewCell
中有兩個彼此相鄰的UILabel
視圖。左邊有一條線,右邊可以有多條線並使用剩下的任何水平空間。兩個標籤距離單元格頂部的距離相同。單元格的高度由正確標籤的高度決定。在某些情況下,我發現上面和下面有不需要的額外空間,因此它們不是頂端對齊的。仔細研究它,我發現hasAmbiguousLayout
爲這兩個標籤返回YES
。UITableViewCell中UILabels的模糊佈局警告
當我在調試器中調用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:下面是我對內容查看的約束和兩個標籤:
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>"
)
這裏是在我有約束的畫面,什麼不順心:
末已經做了你在xib/storyboard或代碼中進行佈局? – Cocoadelica 2015-02-24 14:07:46
在代碼中,使用PureLayout – Koen 2015-02-24 14:15:09