Real iPhone XR screen resolution differs from screen resolution given by swift?

5.5k Views Asked by At

iPhone XR screen resolution according to screenshots, apple website and etc.:

828x1792

iPhone XR screen resolution according to Swift:

375x812 (or 750x1624)

Why and how to deal with it?!

NOTE: all iphones Xx work properly except of this one - iphone XR (+iphone XR simulator)

4

There are 4 best solutions below

2
On

There are two different concepts: UIKit size and Native resolution.

UIKit size is represented by points and is used when you're positioning views inside your project. Let's say your screen is has size 414x896 points, and you center your view to position [207,448], your view will be in the center of the screen.

Native resolution is represented by pixels and it's telling how much pixels screen of the device has. You can get native resolution of device by multiplying UIKit size with Native Scale factor which is individual for each device.

I created table for these sizes, sclae factors and resolution for all iPhones which supports iOS 12

iPhone       | UIKit size (points) | Native resolution (pixels) | Native Scale factor
-------------|---------------------|----------------------------|--------------------
Xs Max       | 414x896             | 1242x2688                  | 3
X, Xs        | 375x812             | 1125x2436                  | 3
Xr           | 414x896             | 828x1792                   | 2
6+,6s+,7+,8+ | 414x736             | 1080x1920                  | 2.608
6,6s,7,8     | 375x667             | 750x1334                   | 2
5s,SE        | 320x568             | 640x1136                   | 2

... from this table you can see that your iPhone Xr has UIKit size 414x896, not 375x812 as your wrote in your question.

You can get device's UIKit size by getting screen's bounds

UIScreen.main.bounds

and native resolution by getting native bounds

UIScreen.main.nativeBounds

... each of these works well for iPhone Xr so check once again if you're running your code on iPhone Xr and not on iPhone Xs/X which has UIKit size 375x812. Also this 750x1624 is just some weird scale of Xr' native resolution, so check if you're not doing any extra math.

3
On

You are getting the iphone X resolution, and that happens most probably because you are using the iphone X in storyboard. The code gives you the storyboard resolution first. If you want to get the real size, call layoutIfNeeded() method before the code that uses the resolution

2
On

There is a difference between the physical screen size and the UIKit size (pixels vs points).

UIKit uses points, a point can comprise of multiple pixels (this is where @2x, @3x) comes in.

The main difference between the XR versus the other X devices is that the XR uses @2x and the other devices are @3x.

Here are Apple's docs on this: https://developer.apple.com/library/archive/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/Displays/Displays.html

PaintCode have also made a really handy graphic to illustrate this:

https://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions

EDIT to answer rmaddy's comment.

If the storyboard is setup to show iPhone X then that's the size that will be returned until the views have been laid out.

1
On

This is because 'Display Zoom' is enabled on your iPhone XR.

Settings -> Display & Brightness -> Display Zoom -> Set to Standard