【问题标题】:How to resize UIImageView in Swift如何在 Swift 中调整 UIImageView 的大小
【发布时间】:2025-12-17 05:00:01
【问题描述】:

我的 UITableView 单元格中有一个图像,但对于某些单元格,我不想显示图像,所以我决定将图像高度大小设置为零(你知道我不能使用 Hidden=true),因为剩余的空白空间。

所以,我尝试了所有方法,但无法调整图像大小,这是我的代码,那么如何调整大小?

    cell!.imageImage.layer.frame = CGRectMake(0, 0, 0, 0)
    cell!.imageImage.layer.masksToBounds = true                  

我也试过了:

    cell!.imageImage.frame = CGRectMake(0, 0, 0, 0)
    cell!.imageImage.layer.masksToBounds = true

那我该怎么办?

【问题讨论】:

  • 这个单元格是如何实现的?是否使用了自动布局?
  • @Kirsteins 是的,我也使用了一些约束。
  • cell!.imageImage.layer.frame = CGRectMake(0, 0, cell.frame.width - 你的图像宽度,cell.frame.height - 你的图像高度) 试试这个..
  • @DharmeshKheni 仍然没有工作。
  • 你试过将 image 设置为 nil 吗?也许使 imageView 的背景透明?还能提供截图吗?回答起来会容易得多

标签: ios uitableview swift uiimageview


【解决方案1】:

如果您使用默认的UITableViewCellUITableViewCellStyleDefault,则必须继承UITableViewCell 并实现layoutSubviews 方法,并在那里设置图像的框架。

【讨论】: