ios 以点值检测 iPhone 6/6+ 屏幕尺寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25756087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:18:59  来源:igfitidea点击:

Detecting iPhone 6/6+ screen sizes in point values

iosiphoneios8

提问by daspianist

Given the newly announced iPhone 6 screen sizes:

鉴于新发布的 iPhone 6屏幕尺寸

iPhone 6: 1334h * 750w @2x (in points: 667h * 375w)
iPhone 6+: 1920 * 1080 @3x (in points: 640h * 360w)

I was wondering if there is code that allows me to detect which screen size the user's device is, so that I could adjust and size UIImagesand other materials accordingly with the user's device.

我想知道是否有代码可以让我检测用户设备的屏幕尺寸,以便我可以UIImages根据用户的设备调整大小和其他材料。

So far, I have been using the following:

到目前为止,我一直在使用以下内容:

- (NSString *) platform{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithUTF8String:machine];
    free(machine);
    return platform;
}

- (NSString *) platformString{
    NSString *platform = [self platform];
    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
    if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
    if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
    if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
    if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (GSM)";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (GSM)";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
    if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
    if ([platform isEqualToString:@"iPad4,4"])      return @"iPad mini 2G (WiFi)";
    if ([platform isEqualToString:@"iPad4,5"])      return @"iPad mini 2G (Cellular)";
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
    return platform;
}

As such, should I assume iPhone7,1and iPhone7,2are the iPhone 6 while iPhone7,3and iPhone7.4are the pluses? If anyone has more concrete way to tell it'd be great, thanks.!

因此,我应该承担iPhone7,1iPhone7,2是iPhone 6,同时iPhone7,3iPhone7.4是加号?如果有人有更具体的方法来告诉它会很棒,谢谢。!

回答by Sverrisson

The first screen will be the device screen, Note that a launch images for the new phones have to be added before, otherwise the app is running in Zoomed Mode for older apps : Here is the code I used to check this out. Note: This only works with version iOS 8 and higher:

第一个屏幕将是设备屏幕,请注意,必须先添加新手机的启动图像,否则旧应用程序的应用程序将在缩放模式下运行:这是我用来检查的代码。注意:这仅适用于 iOS 8 及更高版本

UIScreen *mainScreen = [UIScreen mainScreen];
NSLog(@"Screen bounds: %@, Screen resolution: %@, scale: %f, nativeScale: %f",
          NSStringFromCGRect(mainScreen.bounds), mainScreen.coordinateSpace, mainScreen.scale, mainScreen.nativeScale);

Code for detecting iPhone 6 Plus:

检测 iPhone 6 Plus 的代码:

#define IS_PAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_PHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

-(BOOL)iPhone6PlusDevice{
    if (!IS_PHONE) return NO;
    if ([UIScreen mainScreen].scale > 2.9) return YES;   // Scale is only 3 when not in scaled mode for iPhone 6 Plus
    return NO;
}

or

或者

-(BOOL) iPhone6PlusUnZoomed{
    if ([self iPhone6PlusDevice]){
        if ([UIScreen mainScreen].bounds.size.height > 720.0) return YES;  // Height is 736, but 667 when zoomed.
    }
    return NO;
}

Note: If you are checking for iPhone 6 Plus, to adjust the user interface then don′t rely on .nativeScale, because the simulator and actual device give different results. Due to the comment below. Scale is a CGFloat and thus, code should not check equality, because some floats values may never be equal.

注意:如果您正在检查 iPhone 6 Plus,请不要依赖于调整用户界面.nativeScale,因为模拟器和实际设备给出的结果不同。由于下面的评论。Scale 是一个 CGFloat,因此,代码不应该检查相等性,因为一些浮点值可能永远不会相等。



After adding Launch Screenyou can utilise the new iPhone sizes, otherwise your app will still look scaled.

添加Launch Screen 后,您可以使用新的 iPhone 尺寸,否则您的应用程序看起来仍会缩放。

Updated for the new iPhones 11, 11 Pro and 11 Pro Max

针对新款 iPhone 11、11 Pro 和 11 Pro Max 进行了更新

Size for iPhone 11 Pro Maxwith @3xscaling, coordinate space: 414 x 896points and 1242 x 2688pixels, 458 ppi, device physical size is 3.06 x 6.22 in or 77.8 x 158.0 mm. 6.5" Super Retina XDR display.

iPhone 11 Pro Max尺寸,@3x缩放,坐标空间:414 x 896点和1242 x 2688像素,458 ppi,设备物理尺寸为 3.06 x 6.22 英寸或 77.8 x 158.0 毫米。6.5" 超级视网膜 XDR 显示屏。

Size for iPhone 11 Prowith @3xscaling, coordinate space: 375 x 812points and 1125 x 2436pixels, 458 ppi, device physical size is 2.81 x 5.67 in or 71.4 x 144.0 mm. 5.8" Super Retina XDR display.

iPhone 11 Pro尺寸,@3x缩放,坐标空间:375 x 812点和1125 x 2436像素,458 ppi,设备物理尺寸为 2.81 x 5.67 英寸或 71.4 x 144.0 毫米。5.8 英寸超级视网膜 XDR 显示屏。

Size for iPhone 11with @2xscaling, coordinate space: 414 x 896points and 828 x 1792pixels, 326 ppi, device physical size is 2.98 x 5.94 in or 75.7 x 150.9 mm. 6.1" Liquid Retina HD display.

iPhone 11尺寸,@2x缩放,坐标空间:414 x 896点和828 x 1792像素,326 ppi,设备物理尺寸为 2.98 x 5.94 英寸或 75.7 x 150.9 毫米。6.1 英寸 Liquid 视网膜高清显示屏。

Size for iPhone X Maxwith @3xscaling (Apple name: Super Retina HD 6.5 display"), coordinate space: 414 x 896points and 1242 x 2688pixels, 458 ppi, device physical size is 3.05 x 6.20 in or 77.4 x 157.5 mm.

大小为iPhone X最大值@ 3×缩放(苹果名:超级视网膜HD 6.5显示”),坐标空间:414 X 896点和1242 X 2688个像素,458个PPI,设备的物理尺寸是3.05 X 6.20或77.4 X157.5毫米.

let screen = UIScreen.main
print("Screen bounds: \(screen.bounds), Screen resolution: \(screen.nativeBounds), scale: \(screen.scale)")
//iPhone X Max Screen bounds: (0.0, 0.0, 414.0, 896.0), Screen resolution: (0.0, 0.0, 1242.0, 2688.0), scale: 3.0

Size for iPhone Xwith @2xscaling (Apple name: Super Retina HD 6.1" display), coordinate space: 414 x 896points and 828 x 1792pixels, 326 ppi, device physical size is 2.98 x 5.94 in or 75.7 x 150.9 mm.

大小为iPhone X@ 2X缩放(苹果名:超级视网膜HD 6.1"显示器),坐标空间:414 X 896点和828 X 1792像素,326 PPI,设备的物理尺寸是在2.98 X 5.94或75.7 X150.9毫米。

let screen = UIScreen.main
print("Screen bounds: \(screen.bounds), Screen resolution: \(screen.nativeBounds), scale: \(screen.scale)")
//iPhone X Screen bounds: (0.0, 0.0, 414.0, 896.0), Screen resolution: (0.0, 0.0, 828.0, 1792.0), scale: 2.0

Size for iPhone Xand iPhone Xwith @3xscaling (Apple name: Super Retina HD 5.8" display), coordinate space: 375 x 812points and 1125 x 2436pixels, 458 ppi, device physical size is 2.79 x 5.65 in or 70.9 x 143.6 mm.

尺寸为iPhone XiPhone X@ 3倍缩放(苹果名称:超级视网膜高清5.8"显示屏),坐标空间:375 X 812点和1125 X 2436个像素,458 PPI,设备的物理尺寸为2.79 X 5.65或70.9 X 143.6 毫米。

let screen = UIScreen.main
print("Screen bounds: \(screen.bounds), Screen resolution: \(screen.nativeBounds), scale: \(screen.scale)")
//iPhone X and X Screen bounds: (0.0, 0.0, 375.0, 812.0), Screen resolution: (0.0, 0.0, 1125.0, 2436.0), scale: 3.0

Size for iPhone 8, 7, 6 Plusand iPhone 8, 7, 6S Pluswith @3x scaling (Apple name: Retina HD 5.5), Coordinate space: 414 x 736points and 1242 x 2208pixels, 401 ppi, screen physical size is 2.7 x 4.8 in or 68 x 122 mm:

尺寸为iPhone 8,7,6加iPhone 8,7,6S加用@ 3倍缩放(苹果名称:视网膜HD 5.5),坐标空间:414 X 736点和1242×2208个像素,401 PPI,屏幕物理尺寸2.7 x 4.8 英寸或68 x 122 毫米

Screen bounds: {{0, 0}, {414, 736}}, Screen resolution: <UIScreen: 0x7f97fad330b0; bounds = {{0, 0}, {414, 736}}; 
mode = <UIScreenMode: 0x7f97fae1ce00; size = 1242.000000 x 2208.000000>>, scale: 3.000000, nativeScale: 3.000000


Size for iPhone 6and iPhone 6Swith @2x scaling (Apple name: Retina HD 4.7), Coordinate space: 375 x 667points and 750 x 1334pixels, 326 ppi, screen physical size is 2.3 x 4.1 in or 58 x 104 mm:

iPhone 6iPhone 6S尺寸@2x 缩放(Apple 名称:Retina HD 4.7),坐标空间:375 x 667点和750 x 1334像素,326 ppi,屏幕物理尺寸为 2.3 x 4.1 英寸或58 x 104 毫米

Screen bounds: {{0, 0}, {375, 667}}, Screen resolution: <UIScreen: 0x7fa01b5182d0; bounds = {{0, 0}, {375, 667}}; 
mode = <UIScreenMode: 0x7fa01b711760; size = 750.000000 x 1334.000000>>, scale: 2.000000, nativeScale: 2.000000

And iPhone 5for comparison is 640 x 1136, iPhone 4640 x 960.

iPhone 5的对比是640×1136,iPhone 4的640×960。

Note: Upload LaunchImages otherwise the app will run scaled and not show correct scaling, or screen sizes.

注意:上传 LaunchImages 否则应用程序将按比例运行并且不会显示正确的比例或屏幕尺寸。

Comparing iPhone 6 and 6 Plus

比较 iPhone 6 和 6 Plus

回答by bkhayll

If you prefer macros here are the ones, that you can use to differentiate between the iPhone models. These are based on the point values.

如果您更喜欢这里的宏,您可以使用它们来区分 iPhone 型号。这些是基于点值。

#define IS_IPHONE_4 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)480) < DBL_EPSILON)
#define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSILON)
#define IS_IPHONE_6 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)667) < DBL_EPSILON)
#define IS_IPHONE_6_PLUS (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)736) < DBL_EPSILON)

回答by Roland Keesom

I use the following code to determine what device is running (it's a little bit quick and dirty but it does the trick)

我使用以下代码来确定正在运行的设备(它有点快速和肮脏,但它确实有效)

if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ){

    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    if( screenHeight < screenWidth ){
        screenHeight = screenWidth;
    }

    if( screenHeight > 480 && screenHeight < 667 ){
        NSLog(@"iPhone 5/5s");
    } else if ( screenHeight > 480 && screenHeight < 736 ){
        NSLog(@"iPhone 6");
    } else if ( screenHeight > 480 ){
        NSLog(@"iPhone 6 Plus");
    } else {
        NSLog(@"iPhone 4/4s");
    }
}

(This only works when iPhone 6 / 6 Plus is enabled by adding the appropriate splash screens)

(这仅在通过添加适当的闪屏启用 iPhone 6 / 6 Plus 时有效)

回答by Jeremy Huddleston Sequoia

On the physical device, iPhone 6 Plus's main screen's bounds is 2208x1242 and nativeBounds is 1920x1080. There is hardware scaling involved to resize to the physical display.

在实体设备上,iPhone 6 Plus 的主屏边界为 2208x1242,nativeBounds 为 1920x1080。调整物理显示器的大小需要硬件缩放。

On the simulator, the iPhone 6 Plus's main screen's bounds and nativeBounds are both 2208x1242.

在模拟器上,iPhone 6 Plus 的主屏bounds 和nativeBounds 都是2208x1242。

In other words... Videos, OpenGL, and other things based on CALayers that deal with pixels will deal with the real 1920x1080 framebuffer on device (or 2208x1242 on sim). Things dealing with points in UIKit will be deal with the 2208x1242 (x3) bounds and get scaled as appropriate on device.

换句话说......视频、OpenGL 和其他基于处理像素的 CALayer 的东西将处理设备上真实的 1920x1080 帧缓冲区(或 2208x1242 的 sim)。在 UIKit 中处理点的事情将处理 2208x1242 (x3) 边界并在设备上适当缩放。

The simulator does not have access to the same hardware that is doing the scaling on device and there's no really much of a benefit to simulating it in software as they'd produce different results than the hardware. Thus it makes sense to set the nativeBounds of a simulated device's main screen to the bounds of the physical device's main screen.

模拟器无法访问在设备上进行缩放的相同硬件,并且在软件中模拟它并没有太大的好处,因为它们会产生与硬件不同的结果。因此,将模拟设备主屏幕的 nativeBounds 设置为物理设备主屏幕的边界是有意义的。

iOS 8 added API to UIScreen (nativeScale and nativeBounds) to let a developer determine the resolution of the CADisplay corresponding to the UIScreen.

iOS 8 在 UIScreen 中添加了 API(nativeScale 和 nativeBounds),让开发者决定 UIScreen 对应的 CADisplay 的分辨率。

回答by sweepy_

You can detect iPhone 6 Plus based on its native scale, using this macro:

您可以使用以下宏根据 iPhone 6 Plus 的原始比例来检测它:

#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_6PLUS (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f)

回答by user3344236

Check the updated list on wiki, there I got 7,2 for iPhone 6 and 7,1 for iPhone 6 plus.

查看wiki上的更新列表,iPhone 6 为 7,2,iPhone 6 plus 为 7,1。

回答by Paul Buchanan

Hannes Sverrisson's answer is almost correct. The iPhone 6 coordinate system is in fact larger than the 5s Using his code:

Hannes Sverrisson 的回答几乎是正确的。iPhone 6 坐标系实际上比 5s 大 使用他的代码:

UIScreen *mainScreen = [UIScreen mainScreen];
NSLog(@"Screen bounds: %@, Screen resolution: %@, scale: %f, nativeScale: %f",
      NSStringFromCGRect(mainScreen.bounds), mainScreen.coordinateSpace, mainScreen.scale, mainScreen.nativeScale);

The coordinate system for apps providing the correct launch images are:

提供正确启动图像的应用程序的坐标系是:

Size for iPhone 6 (Retina HD 4.7) with @2x scaling, Coordinate space: 375 x 667 and 750 x 1334 actual points:

iPhone 6 (Retina HD 4.7) 尺寸,@2x 缩放,坐标空间:375 x 667 和 750 x 1334 实际点:

Screen bounds: {{0, 0}, {375, 667}}, Screen resolution: <UIScreen: 0x7fa01b5182d0; bounds = {{0, 0}, {375, 667}}; 
mode = <UIScreenMode: 0x7fa01b711760; size = 750.000000 x 1334.000000>>, scale: 2.000000, nativeScale: 2.000000

Size for iPhone 6 Plus (Retina HD 5.5) with @3x scaling, Coordinate space: 414 x 736 and 1242 x 2208 actual points:

iPhone 6 Plus (Retina HD 5.5) 尺寸,@3x 缩放,坐标空间:414 x 736 和 1242 x 2208 实际点:

Screen bounds: {{0, 0}, {414, 736}}, Screen resolution: <UIScreen: 0x7f97fad330b0; bounds = {{0, 0}, {414, 736}}; 
mode = <UIScreenMode: 0x7f97fae1ce00; size = 1242.000000 x 2208.000000>>, scale: 3.000000, nativeScale: 3.000000

回答by David L

This is what I use in my app with iOS 8:

这是我在 iOS 8 的应用程序中使用的内容:

window=[[[UIApplication sharedApplication] windows] firstObject];

NSLog(@"screenHeight=%f width=%f",window.frame.size.height,window.frame.size.width);

if (window.frame.size.height == 480) {

        do stuff here... 
    }

Prior to Xcode6 / iOS 8, I used this, but screen bounds does not work properly with the resizable simulator or at least it didn't in the Xcode6 betas...

在 Xcode6 / iOS 8 之前,我使用过这个,但屏幕边界在可调整大小的模拟器中无法正常工作,或者至少在 Xcode6 beta 中没有......

CGRect screenBounds=[[UIScreen mainScreen] bounds];

if (screenBounds.size.height >= 568) {

do stuff here...

}

回答by Tommy

All three devices have (pretty much) the same number of points per inch. So your images will automatically be the same physical size.

所有三种设备每英寸具有(几乎)相同的点数。因此,您的图像将自动具有相同的物理尺寸。

Use [[UIScreen mainScreen] bounds]to get the total number of points on the screen. Divide by 163 to get the approximate size in inches if you really want it.

使用[[UIScreen mainScreen] bounds]让屏幕上的点的总数。如果您真的需要,除以 163 以获得大约英寸大小。

Notice that the 6+ does not return 1080p because it doesn't render to a 1080p buffer. It renders such that output is approximately 160 points per inch, using @3x assets.

请注意,6+ 不会返回 1080p,因为它不会渲染到 1080p 缓冲区。它使用@3x 资产进行渲染,使输出大约为每英寸 160 点。

No need to second guess.

无需再猜测。

E.g. if you write this code:

例如,如果您编写此代码:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 163, 163)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];

You'll get a view that is pretty much the same physical size — one inch square — on all iOS devices.

您将在所有 iOS 设备上获得几乎相同的物理尺寸(一平方英寸)的视图。

Apple has already done the hard work, so you don't have to.

Apple 已经完成了艰苦的工作,因此您不必这样做。

回答by Sven

I had to detect the iPhone 6 Plus in an app built with iOS 7. Because nativeScale isn't available on [UIScreen mainScreen] I tried to use [UIScreen mainScreen] scale] but this just returned 2.0. So I came up with this solution to detect the iPhone 6 Plus on iOS 7 (should also work on iOS 8):

我必须在使用 iOS 7 构建的应用程序中检测到 iPhone 6 Plus。因为 [UIScreen mainScreen] 上没有 nativeScale,我尝试使用 [UIScreen mainScreen] scale] 但这只是返回 2.0。所以我想出了这个解决方案来检测 iOS 7 上的 iPhone 6 Plus(也应该适用于 iOS 8):

-(BOOL)iPhone6Plus{
BOOL isiPhone6Plus = NO;
SEL selector = NSSelectorFromString(@"scale");
if ([[UIScreen mainScreen] respondsToSelector:selector]) {
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
                                [[[UIScreen mainScreen] class] instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[UIScreen mainScreen]];
    [invocation invoke];
    float returnValue;
    [invocation getReturnValue:&returnValue];
    if (returnValue == 3.0) {
        isiPhone6Plus = YES;
    }
    NSLog(@"ScaleFactor %1.2f", returnValue);
}
return isiPhone6Plus;

}

}

The interesting part of this code is, that if I use NSInvocation the return value of the scale selector will be 3.0. Calling this method directly on iOS 7 returns 2.0.

这段代码有趣的部分是,如果我使用 NSInvocation,比例选择器的返回值将是 3.0。在 iOS 7 上直接调用此方法返回 2.0。