调整Uitable ViewCell内部的自动划分



所以我正在创建一个论坛。当帖子包含图像时,我想保留下载图像的纵横比,同时使图像的宽度在该单元格宽度的75%保持不变。

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

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

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

目前,我对图像宽度的限制为Cell宽度的75%,高度等于200。

我还拥有所有代码来自动调整tableviewcell,这取决于内容,而且工作正常。

感谢您的任何帮助。

尝试

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

最新更新