2017-03-15 72 views
0

所以我正在创建一个论坛。当一篇文章包含图片时,我希望保持下载图片的宽高比,同时使图片的宽度保持在单元格宽度的75%。调整UITableViewCell里面的自动布局

我在我的自定义单元类中为imageHeightConstraint创建了一个插座,并在cellForRowAtIndexPath方法中尝试了下面的代码。

let newHeight = (image!.size.height/image!.size.width)*cell.uploadedImageView.frame.width 
cell.imageHeightConstraint.constant = newHeight//Calculate the new height 

上面的代码根本不调整图像大小。

目前我有图像宽度的限制是单元格的宽度和高度等于200

我也已经把所有的代码调整tableviewcell自动根据内容和工作正常的75%。

感谢您的任何帮助。

回答

0

尝试

if let image = image { 
    cell.image.heightAnchor.constraint(equalTo:cell.image.widthAnchor, multiplier: image.size.height/image.size.width).isActive = true 
} 

如果你也想限制高度为200,那么你也可以添加

cell.image.heightAnchor.constraint(lessThanOrEqualToConstant:200).isActive=true