2013-10-14 35 views
7

我有一個UITableViewCell自定義標籤使用AutoLayout。它的約束條件是在內容視圖中設置一個不變的前導,尾隨,底部和頂部空間。這些單元格出現的表視圖控制器在UISplitViewController內。當表視圖被加載,我計算使用以下代碼的單元格的高度:tableview:heightForRowAtIndexPath:AutoLayout報告內容視圖大小不正確

CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 
CGFloat height = size.height; 

在畫像這允許我以正確顯示的單元格。這裏是小區的一部分截圖:

​​

然而,當我旋轉iPad變成風景,它不計算正確的大小和過多的填充結束:

enter image description here

當旋轉內容視圖的大小時調用tableview:heightForRowAtIndexPath:時未設置正確報告的斷點。實際上它正在報告表格視圖的舊的,更窄的大小。

如何讓我的單元格在iPad上與UISplitViewController適當調整大小?

+0

它看起來像你的單元格包含多行UILabels。爲了獲得正確的高度,標籤將需要正確設置「preferredMaxLayoutWidth」屬性到可用寬度(這可能在設備旋轉時有所不同)。如果你還沒有看過這篇文章,請在評論中查看它以及GitHub上的示例項目:http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic -cell-layouts-heights – smileyborg

+0

您是否創建額外的代碼並將其中的任何控件放入其中?是否可以在此處添加代碼? –

回答

3

UILabel#preferredMaxLayoutWidth影響systemLayoutSizeFittingSize。例如, 。您可以將UILabel放在帶界面生成器的視圖上。

Label Layout

  • 上海華的寬度爲320像素。
  • UILabel的水平空間約束是20px。

在這種情況下,UILabel#preferredMaxLayoutWidth將被設置爲280px。即使superview的frame.width爲480px,systemLayoutSizeFittingSize也會返回不超過320px的寬度。

其中一個解決方案是覆蓋CustomCell#setFrame方法。

- (void)setFrame:(CGRect)frame { 
    [super setFrame:frame]; 
    _label.preferredMaxLayoutWidth = frame.size.width - 20*2 /*Horizontal Spacing*/; 
} 

然後當更新label.text時需要更新佈局。

_customCell.label.text = @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 
[_customCell setNeedsLayout]; 
[_customCell layoutIfNeeded]; 
相關問題