图像的渲染大小与帧高度不匹配 (Swift)

Rendered Size of Image Doesn't Match Frame Height (Swift)

我提前道歉,因为这感觉很简单,但我已经想了几天,但经过大量搜索后还是无法弄清楚。

当我检查以编程方式创建的带有约束的图像的高度时,发现尺寸与其渲染尺寸不匹配。

// Earth Image Setup
view.addSubview(earthImageView)
earthImageView.bottomAnchor.constraint(equalTo: loginTextViewTitle.topAnchor, constant: -standardSpacer).isActive = true
earthImageView.widthAnchor.constraint(equalToConstant: 500).isActive = true
earthImageView.heightAnchor.constraint(equalToConstant: 500).isActive = true
earthImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

// Value of earthImageViewHeight is 276.  Why isn't it 500?
let earthImageViewHeight = earthImageView.frame.size.height

我看到 276 来自原始 @1x 图像的像素高度。但是,如何获得实际渲染高度?

我认为这可能与points vs pixels有关,但似乎也不是这样。

原因是视图尚未完全展开。布局好之后就可以使用约束来设置其他约束了,这就是上面问题背后的问题。

这个 post and this post 很好地解释了正在发生的事情以及如何管理它。对于我的特殊情况调用...

self.view.layoutIfNeeded()

...在我的动画块开始之前,然后重新调整我正在制作动画的图像相对于另一个已经布置好的图像的大小。