Imageview 在运行时更改位置 - Swift

Imageview changes location on runtime - Swift

您好,

我在自定义 table 视图单元格中设置的 imageView 在 运行 时间将设置的图像移动到不同的位置。

让我感到困惑的是,如果我在属性检查器中手动设置图像,图像就会出现在它应该出现的位置。只有在以编程方式设置图像时才会出现此问题。

下面是我使用单元格创建函数生成刻度线或十字线的代码。

// Cell Information

    if answersBool[indexPath.row] == false {
        cell.imageView!.image = UIImage(named: "crossIcon.png")
    } else {
        cell.imageView!.image = UIImage(named: "tickIcon.png")
    }

这是 运行 时间发生的事情。

这是单元格在我的情节提要中的样子。

您需要 constraints 为故事板中的 imageView

试着把它贴在右边。

首先从您的 imageView 中删除所有约束,然后 select 您的 imageView 并单击图钉菜单,然后添加如下图所示的这五个约束:

希望对您有所帮助。