xcode - UIImageView 中的空白区域

标签 xcode swift tableview uitableview

我有一个表格 View ,其中有一些单元格,每个单元格都有一个图像。

图片大小不一。所以当我制作一个单元格时,我必须给 UIImageView 一个宽度和高度的约束。

现在我想要的是:当图像加载到单元格的 UIImageView 中时,UIImageView 的大小应根据图像本身进行调整。

过去 2 天我正在试验,但无法修复它。由于限制,表格中的单元格一直有空格。

我用来填充单元格的代码:

class ProductTableViewCell: UITableViewCell {


@IBOutlet weak var productImageView: UIImageView!
@IBOutlet weak var productTitleLabel: UILabel!
@IBOutlet weak var productDescriptionLabel: UILabel!
var orgframe = UIImageView()


func configureCellWith(product: Product){
    orgframe.frame.size = CGSizeMake(400, 600)
    productImageView.image = product.image
    productDescriptionLabel.text = product.description
    productTitleLabel.text = product.title

    var urlstring = product.imageURL

    ImageLoader.sharedLoader.imageForUrl(urlstring as String, completionHandler:{(image: UIImage?, url: String) in
        //self.productImageView.frame.size = (image?.size)!

        self.productImageView.image = image!


    })


}


}

UIImageview 的约束: enter image description here

最佳答案

给 productImageView 一个固定的高度约束,并将它连接到一个 IBOutlet。然后,当您准备好加载图片时,您可以这样做:

self.productImageHeightConstraint.constant = image.size.height
self.view.layoutIfNeeded()

您需要确保将周围的 View /标签固定到 ImageView 的边缘,以便它们根据图像大小自行排列。

关于xcode - UIImageView 中的空白区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30988982/

相关文章:

c++ - 带有 c++17 的 `filesystem` 在我的 mac os x high sierra 上不起作用

Swift - 如何在结构中初始化数组?

initialization - 在 super.init() 之前或之后我无法快速初始化东西

xcode - 如何动态设置应用程序的内部版本号而不弄脏源代码树?

xcode - MonoDevelop:安装到 SSD 驱动器以提高速度

ios - 如何停止在横向模式下使用单元格向上滚动表格 View 标题?

ios - 表格 View 单元格中的按钮没有响应

ios - Swift 动态表格文本超出单元格宽度

ios - UITextField border-bottom 不会完全显示

Swift 4 从后退按钮中删除文本