【发布时间】:2025-12-23 02:45:11
【问题描述】:
我正在使用带有 Xcode 8 的 Swift 3.0,但我在调整图像大小时遇到了一些问题。这是我设置 UIImage 框架的代码。
open func setData(_ data: Any?, image: String) {
self.textLabel?.font = UIFont.boldSystemFont(ofSize: 10)
self.textLabel?.textColor = UIColor(hex: "000000")
if let menuText = data as? String {
self.textLabel?.text = menuText
}
self.imageView?.contentMode = UIViewContentMode.scaleToFill
self.imageView?.frame = CGRect(x: 12, y: 8, width: 10, height: 10)
self.imageView?.image = UIImage(named: image)
self.imageView?.clipsToBounds = true
}
这是我在 tableView 中调用函数的地方。我对设置图像没有任何问题,但只有调整大小。当我使用较小的图像时,图像会更小,当我使用较大的图像时,图像会变大,它不会调整大小。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = BaseTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: BaseTableViewCell.identifier)
cell.setData(menus[indexPath.row], image: images[indexPath.row])
return cell
}
【问题讨论】:
-
如果这是默认表格单元格内的 imageView,那么它可能有一些默认约束。您是否尝试过创建自己的单元子类?
-
@mag_zbc 现在我要试试这个
标签: ios swift3 uiimageview xcode8