iOS get physical screen size programmatically?
Asked Answered
B

11

36

Is this possible? I want the number of inches, not the number of pixels. I know it is approximately 160 ppi. But not exactly.

Bicarbonate answered 24/11, 2011 at 13:36 Comment(3)
You may be aware of this, but basing your code on the physical characteristics of the device may well cause issues in the future. If the next iPhone has a slightly bigger screen but keeps the same resolution (as some rumors have suggested) you code will be broken on that device.Aril
There's a way to get the screen size on OS X, I wonder if it'll work on iOS?Gombosi
@Gombosi It's been a while since you commented, but CGDisplayScreenSize is available only on macOS, sadlyDebbi
U
10

If it were available it would be in UIScreen or UIDevice but it is not there.

You can infer it from info in Erica's UIDevice-extension and the specs for each device listed here on Wikipedia.

Ursala answered 24/11, 2011 at 14:8 Comment(0)
B
57

There isn't an API that will give you this. Your best bet is to look at the device's screen size (in points) and from that surmise if it's an iPad or iPhone etc., and then use hard-coded values for the screen sizes.

Here's some code to get the screen size:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

Be aware that width and height might be swapped, depending on device orientation.

Bushranger answered 24/11, 2011 at 14:5 Comment(0)
U
10

If it were available it would be in UIScreen or UIDevice but it is not there.

You can infer it from info in Erica's UIDevice-extension and the specs for each device listed here on Wikipedia.

Ursala answered 24/11, 2011 at 14:8 Comment(0)
P
4

Here's a short method that estimates the device screen size. It's updated as to the latest devices, but may fail on future ones (as all methods of guessing might). It will also get confused if the device is being mirrored (returns the device's screen size, not the mirrored screen size)

#define SCREEN_SIZE_IPHONE_CLASSIC 3.5
#define SCREEN_SIZE_IPHONE_TALL 4.0
#define SCREEN_SIZE_IPAD_CLASSIC 9.7

+ (CGFloat)screenPhysicalSize
{
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if (result.height < 500)
            return SCREEN_SIZE_IPHONE_CLASSIC;  // iPhone 4S / 4th Gen iPod Touch or earlier
        else
            return SCREEN_SIZE_IPHONE_TALL;  // iPhone 5
    }
    else
    {
        return SCREEN_SIZE_IPAD_CLASSIC; // iPad
    }
} 
Prognosticate answered 10/10, 2012 at 23:22 Comment(2)
Of course, the introduction of the iPad Mini breaks this code (it's physical size is smaller than the iPad 2, but the same resolution). This can be a guide, but not a guarantee..Prognosticate
Nowadays you have iPhone 6 and 6+, which the user can run in "Zoom" mode, so the same phone can report different values for the bounds.Inane
W
3

You might need use [UIScreen mainScreen].scale;

CGFloat scale = [UIScreen mainScreen].scale;
CGRect screenRect = [[UIScreen mainScreen] bounds];

CGFloat physicalWidth = screenRect.size.width * scale;
CGFloat physicalHeight = screenRect.size.height * scale;
Wyatt answered 7/11, 2014 at 15:36 Comment(1)
That's not correct for iPhone 8 Plus according the this guide. It's logical screen size: 414x736 and it has @3x scale screen, according to your answer it must has physical size 1242x2207 but it has 1080x1920 actually.Sunda
K
0

Since this question has been asked, I’ve created an open-source library to handle this problem: IRLSize. It can be used in either direction: to measure the size of a view (or the whole screen) in real-world dimensions, or to set the size of a view to a specific real-world dimension.

Knurly answered 24/11, 2011 at 14:3 Comment(2)
He may have considered this part of his research. At least help point him in the right direction.Coonskin
@Coonskin This is a two-and-a-half-year-old comment. Come on, man.Knurly
B
0

Maybe Jeff Hay's code can be adapted to include iPad Mini. The trick is to get the device's model identifier. The most recent non-retina iPad is "iPad2,4" and the first iPad mini is "iPad2,5". Now all you need to check is if the screen scaling is 1.0 (non-retina)

Although this code is not future-proof, you can always add more rules for model identifiers.

#import <sys/utsname.h>

#define SCREEN_SIZE_IPAD_MINI 7.9

    struct utsname systemInfo;
    uname(&systemInfo);
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && strcasecmp(systemInfo.machine, "iPad2,5") >= 0 [[UIScreen mainScreen] scale] == 1.0)
        return SCREEN_SIZE_IPAD_MINI
Blocking answered 14/7, 2013 at 13:44 Comment(0)
T
0

The "formula" I use is

#define IS_iPhone5 ( fabs( (double)[ [ UIScreen mainScreen ] bounds ].size.height - (double)568 ) < DBL_EPSILON )
Tarpeia answered 12/6, 2014 at 21:46 Comment(0)
S
0

note: screen rotation matters here

extension UIScreen {
    var physicalSize:CGSize {
        return CGSize(width: bounds.width*scale, height: bounds.height*scale)
    }
}

using:

print(UIScreen.main.physicalSize)
Stenotypy answered 11/11, 2020 at 8:41 Comment(0)
E
-1

Here is a Swift way to get screen sizes:

var screenWidth: CGFloat {
    if UIInterfaceOrientationIsPortrait(screenOrientation) {
        return UIScreen.mainScreen().bounds.size.width
    } else {
        return UIScreen.mainScreen().bounds.size.height
    }
}
var screenHeight: CGFloat {
    if UIInterfaceOrientationIsPortrait(screenOrientation) {
        return UIScreen.mainScreen().bounds.size.height
    } else {
        return UIScreen.mainScreen().bounds.size.width
    }
}
var screenOrientation: UIInterfaceOrientation {
    return UIApplication.sharedApplication().statusBarOrientation
}

These are included as a standard function in:

https://github.com/goktugyil/EZSwiftExtensions

Edging answered 24/11, 2015 at 4:42 Comment(2)
This doesn't answer his question as he specifically says "I want the number of inches, not the number of pixels."Szczecin
None of the answers do in that case. But at least we know how many inches an iphone is so its easy to calculate from hereEdging
C
-1

Nobody said about fixedCoordinateSpace. In Swift 3 to get the screen dimensions in a portrait-up orientation you should use: UIScreen.main.fixedCoordinateSpace.bounds

Carlile answered 18/7, 2017 at 17:31 Comment(0)
C
-2

CGFloat scale = [UIScreen mainScreen].scale; CGRect screenRect = [[UIScreen mainScreen] bounds]; CGFloat screenWidth = screenRect.size.width * (scale/100.0f); CGFloat screenHeight = screenRect.size.height * (scale/100.0f);

  • scale is persent!
Charlesettacharleston answered 16/2, 2016 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.