I find that UIScreen.main.bounds
are really hard to grasp. They are inconsistent, and their values doesn't seem to reflect the device. They are not static but change when the device is rotated which is really confusing, and often the values are the opposit of what you would expect.
For example:
In portrait mode
print("Portrait", " width: ", UIScreen.main.bounds.size.width, " height: ", UIScreen.main.bounds.size.height)
gives
Portrait width: 1024.0 height: 768.0
Correct me if I'm wrong but shouldn't the width be less then the hight on an iPad pro simulator?
Landscape mode is also confusing.
print("landscape", " width: ", UIScreen.main.bounds.size.width, " height: ", UIScreen.main.bounds.size.height)
gives
landscape width: 768.0 height: 1024.0
This means that 1. UIScreen.main.bounds
is not static but change with screen orientation and 2. The width is still less then the hight.
I don't get it. Is there a way to get a static representation of the screen size and one that makes sense?
EDIT
Another strange phenomena is that UIScreen.main.bounds.height
gives 1024
if I print it in viewDidLoad
. But if I flip orientation and then back to the way it was, then restart the simulator the same print gives 768. Why oh why. This is so weird.
Have you consulted the 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.
And then below in the See Also section there's a link to nativeBounds
. Its doc states:
This rectangle is based on the device in a portrait-up orientation. This value does not change as the device rotates.
There you have the static bounds you're looking for.