我正在为iPhone和iPad开发一个应用程序。现在我想在不同的手机屏幕上检索UIImageView的大小。我尝试了以下代码:

print("width \(BubbleBackground.frame.size.width)")
print("height \(BubbleBackground.frame.size.height)")
print("width1 \(BubbleBackground.bounds.size.width)")
print("height1 \(BubbleBackground.bounds.size.height)")
print("width2\(BubbleBackground.image!.size.width)")
print("height2\(BubbleBackground.image!.size.height)")

我试过上面的代码,但是不管它运行在iPhone 5、iPhone 6、iPhone 6 plus或iPad上,它们都会返回一个常量值。有人知道在不同的设备上运行时如何获得图像大小吗?

最佳答案

要根据屏幕大小更改imageView框架,必须有一些限制条件。然后可以使用.layoutIfNeeded()上的UIImageView(BubbleBackground.layoutIfNeeded())获取其运行时大小。
确保您在viewWillAppearviewDidLayoutSubviews中或在这些事件之后的任何时间执行此操作。

关于ios - 以编程方式获取UIImageView的大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38970034/

10-12 06:26