UITableViewCell的高度根据UIImageview的宽高比进行扩展

时间:2019-06-06 09:56:18

标签: ios swift uitableview uiimageview intrinsics

我正在尝试在UITableViewCell中包含一个UIImageview。图像的宽度不应超过设备的安全区域,并且高度的高度不应超过350。问题:图像可以正确缩放,但表格视图的高度不会缩小以紧贴图像少于350。请寻求您的建议

  1. 将imageview宽度设置为414并设置为高优先级,将imageview设置为.scaleaspectFit。
  2. 将图像垂直拥抱优先级设置为<= 350(低优先级)。

我希望找到解决方案或解决方案的指导,以解决图像将不超过设置的高度增长的问题,不知道这是一种好习惯。 同样,当单元高度缩小时,单元也可以收缩并紧贴图像,同时仍根据设备屏幕/安全区域保持恒定的宽度

谢谢大家

更多代码

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let currentImage = images2[indexPath.row]
        let imageCrop = currentImage.getCropRatio()
        return tableView.frame.width / imageCrop

}

extension UIImage {
    func getCropRatio() -> CGFloat {
        var widthRatio = CGFloat(self.size.width / self.size.height)
        return widthRatio
    }
}

0 个答案:

没有答案