为什么 UIScreen.main.bounds 是反转的而不是静态的?

Why is UIScreen.main.bounds reversed and not static?

我发现UIScreen.main.bounds真的很难掌握。它们不一致,并且它们的值似乎无法反映设备。它们不是静态的,而是在设备旋转时发生变化,这确实令人困惑,而且这些值通常与您期望的相反。

例如:

纵向模式

print("Portrait", " width: ", UIScreen.main.bounds.size.width, " height: ", UIScreen.main.bounds.size.height)

给予

Portrait width: 1024.0 height: 768.0

如果我错了请纠正我,但宽度不应该小于 iPad 专业模拟器上的高度吗?

横向模式也令人困惑。

print("landscape", " width: ", UIScreen.main.bounds.size.width, " height: ", UIScreen.main.bounds.size.height)

给予

landscape width: 768.0 height: 1024.0

这意味着 1. UIScreen.main.bounds 不是静态的,而是随着屏幕方向而变化,并且 2. 宽度仍然小于高度。

我不明白。有没有一种方法可以得到屏幕尺寸的静态表示并且是有意义的?

编辑 另一个奇怪的现象是,如果我在viewDidLoad中打印它,UIScreen.main.bounds.height会得到1024。但是如果我翻转方向然后回到原来的方式,然后重新启动模拟器相同的打印输出 768。为什么哦为什么。这太奇怪了。

你咨询过docs on UIScreen.bounds吗?

This rectangle is specified in the current coordinate space, which takes into account any interface rotations in effect for the device. Therefore, the value of this property may change when the device rotates between portrait and landscape orientations.

然后在下面的 另请参阅 部分中有一个 link 到 nativeBoundsIts doc 状态:

This rectangle is based on the device in a portrait-up orientation. This value does not change as the device rotates.

这里有您要查找的静态边界。