How does UIScreen.main.nativeBounds relate to x and y coordinates?

945 Views Asked by At

If I print UIScreen.main.nativeBounds.height it returns 2048 for my iPad simulator which is fine. But how does this relate to the x and y coordinates? For example, the middle of the screen is x: 0, y: 0. And if I move to the left on the x coordinates I use minus, like x: -100, and if I move to the right I use plus, like x: 100.

In my head this would mean that UIScreen.main.nativeBounds.height is divided by 2 so that I can move between x: -1024 and x: 1024. Is that correct? Well, it seems that it's not because this code:

portrait = gui.addPortrait(width: 80, height: 80, x:-350 , y:  180)
addChild(portrait)

will take me to the far end of the device's x-axis, like this:

enter image description here

meaning that the total x-axis must be something like 750 in total. What happened to 1024? I think I must misunderstand how the x-coordinates relate to the UIScreen.main.nativeBounds.height. The reason I want to understand this is because I want to set the x position dynamically so that it works on all devices. Something like x: -(UIScreen.main.nativeBounds.height / 3). What am I missing?

2

There are 2 best solutions below

1
fphilipe On

In contrast to UIScreen.bounds, which specifies "[t]he bounding rectangle of the screen, measured in points" (docs), UIScreen.nativeBounds specifies "[t]he bounding rectangle of the physical screen, measured in pixels" (docs).

Thus, you'll want to transform the values into points with the help of UIScreen.nativeScale:

let width = round(UIScreen.main.nativeBounds.width / UIScreen.main.nativeScale)
let height = round(UIScreen.main.nativeBounds.height / UIScreen.main.nativeScale)
5
LuLuGaGa On

You can get the height and width of the screen in points like this:

let height = UIScreen.main.bounds.height
let width = UIScreen.main.bounds.width

No point of the screen has negative coordinates:

The top left-hand corner of the screen is x: 0, y :0

The top right-hand corner of the screen is x: width, y: 0

The bottom left-hand corner of the screen is x: 0, y: height

The bottom right-hand corner of the screen is x: width, y: height