![](/img/trans.png)
[英]Set UIImageView height proportional to width in UITableViewCell
[英]Set UITableViewCell height proportional to image proportional
我有一張1x的圖片,尺寸為320x130。 我想將UItableViewCell
設置為始終具有此圖像的比例。 我怎樣才能做到這一點? 我試過下面的代碼,但似乎使圖像很小?
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let ratio = UIScreen.mainScreen().bounds.width / 130
let targetHeight = 130 * UIScreen.mainScreen().scale
return targetHeight / ratio
}
而不是UIScreen.mainScreen().bounds
,應使用tableView.bounds
,這與您的情況相同,但是如果更改表視圖,它將不起作用。
至於比率,它是這樣的:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 130.0/320.0 * tableView.bounds.width
}
您可以通過將高度除以寬度來獲得比率,然后將其乘以表格視圖的寬度以得到所需的高度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.