2015-02-24 32 views
0

我在UITableViewCell中有兩個彼此相鄰的UILabel視圖。左邊有一條線,右邊可以有多條線並使用剩下的任何水平空間。兩個標籤距離單元格頂部的距離相同。單元格的高度由正確標籤的高度決定。在某些情況下,我發現上面和下面有不需要的額外空間,因此它們不是頂端對齊的。仔細研究它,我發現hasAmbiguousLayout爲這兩個標籤返回YESUITableViewCell中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。兩個標籤都設置了translatesAutoresizingMaskIntoConstraintsNO

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

這裏是在我有約束的畫面,什麼不順心:

enter image description here

+0

末已經做了你在xib/storyboard或代碼中進行佈局? – Cocoadelica 2015-02-24 14:07:46

+0

在代碼中,使用PureLayout – Koen 2015-02-24 14:15:09

回答

8

我解決它通過添加合適的UILabel

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

的另一件事是,我是爲歧義updateConstraints測試,而我應該在layoutSubviews

+0

謝謝!它幫助了我。 – 2017-10-06 17:44:03

1

啊!您是否關閉了cell.contentView的視圖層次結構中的每個子視圖的 translatesAutoresizingMaskIntoConstraints屬性?

輸出顯示您的自動佈局與默認自動調整行爲之間的衝突證據。在xib中,可以通過複選框關閉所有內容,但在代碼中,您必須在每個視圖級別上執行此操作。

+0

PureLayout框架已經考慮到了這一點。 – Koen 2015-02-24 14:40:18

+0

不是根據您給出的輸出,而是顯式列出自動調整限制條目。 HAS:| - (0) - [UITableViewCellContentView:0x7b768fc0](名稱:'|':MyTVCell:0x7b769330' MyTVCell')>''' – Cocoadelica 2015-02-24 14:54:50

+0

有趣。在我檢查'constraintsAffectingLayoutForAxis','self.bodyLabel'的同一點。translatesAutoresizingMaskIntoConstraints' returns'NO' – Koen 2015-02-24 15:37:14