2016-09-19 46 views
0

我堅持嘗試僅爲所提供的圖像中的頂部標籤設置動態高度。我嘗試過很多東西,但我總是以與內容重疊的行結束。任何幫助,將不勝感激。我用Swift使用AutoLayout。Xcode - 只有頂部標籤的動態高度

xcode_dynamic_height_top_label

UPDATE:

下面是我目前的制約因素:

image2 image3

我在viewDidLoad中的視圖控制器下面的代碼:

notificationsTable.estimatedRowHeight = 85.0 
notificationsTable.rowHeight = UITableViewAutomaticDimension 
+0

請出示您的約束條件和所需輸出你想要什麼實際實現 ? –

+0

你在這個單元格中添加了約束嗎? –

+0

我已更新我的帖子。 –

回答

0

這樣做:

圖像的長文本: enter image description here

短文本: enter image description here

圖片約束:

enter image description here

動態標記約束:

enter image description here

普通文本約束:

enter image description here

希望這有助於

+0

這幾乎可行,我的形象只是切斷了一點,需要調整什麼?我也不想讓自己的形象延伸。 –

+0

您可以嘗試將圖片內容模式設置爲Aspect Fit或Aspect Fill – lee5783

+0

不,我不是這個意思,我的意思是這行是裁剪圖像的底部,並導致圖像重疊在下一行時,只有一行文本。 –

0

使用這種簡單的方法:

func heightForView(_ text:String, width:CGFloat) -> CGFloat{ 
    let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude)) 
    label.numberOfLines = 0 
    label.lineBreakMode = NSLineBreakMode.byWordWrapping 
    label.text = text 
    label.sizeToFit() 
    return label.frame.height 
} 

notificationsTable.rowHeight = heightForView(yourText, width: yourCGFloatWidth)