我有一个表格 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 的约束:
最佳答案
给 productImageView 一个固定的高度约束,并将它连接到一个 IBOutlet。然后,当您准备好加载图片时,您可以这样做:
self.productImageHeightConstraint.constant = image.size.height
self.view.layoutIfNeeded()
您需要确保将周围的 View /标签固定到 ImageView 的边缘,以便它们根据图像大小自行排列。
关于xcode - UIImageView 中的空白区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30988982/