【问题标题】:Adjust Auto-Layout inside UITableViewCell调整 UITableViewCell 内的自动布局
【发布时间】:2017-03-15 15:51:35
【问题描述】:

所以我正在创建一个论坛。当帖子包含图像时,我希望保持下载图像的纵横比,同时使图像的宽度恒定为单元格宽度的 75%。

我在自定义单元格类中为 imageHeightConstraint 创建了一个出口,并在 cellForRowAtIndexPath 方法中尝试了以下代码。

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

上面的代码根本没有调整图片大小。

目前我将图像宽度限制为单元格宽度的 75%,高度等于 200。

我也已经有了根据内容自动调整 tableviewcell 的所有代码,并且工作正常。

感谢您的帮助。

【问题讨论】:

    标签: swift xcode uitableview autolayout


    【解决方案1】:

    试试

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 2011-03-22
      相关资源
      最近更新 更多