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

问题描述 投票:-2回答:1

我提前道歉,因为这感觉很简单,但我一直试图弄清楚这几天,经过大量的搜索后无法弄明白。

当我检查图像的高度时,我用编程方式创建了约束,其大小与它的渲染大小不匹配。

// 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有关,但似乎也不是这样。

ios swift nslayoutconstraint
1个回答
0
投票

原因是该观点还没有完全阐述。一旦它被布局,那么您可以使用约束来设置其他约束,这是上述问题背后的问题。

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

self.view.layoutIfNeeded()

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

© www.soinside.com 2019 - 2024. All rights reserved.