以点值检测iPhone 6/6 +屏幕尺寸 [英] Detecting iPhone 6/6+ screen sizes in point values
问题描述
鉴于新推出的iPhone 6 屏幕尺寸:
Given the newly announced iPhone 6 screen sizes:
iPhone 6: 1334h * 750w @2x (in points: 667h * 375w)
iPhone 6+: 1920 * 1080 @3x (in points: 640h * 360w)
我想知道是否有代码可以让我检测用户设备的屏幕尺寸,以便我可以使用用户的设备相应地调整和调整 UIImages
和其他材料。
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 UIImages
and other materials accordingly with the user's device.
到目前为止,我一直在使用以下内容:
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;
}
因此,我应该假设 iPhone7,1
和 iPhone7,2
是iPhone 6,而 iPhone7,3
和 iPhone7.4
是优点?如果有人有更具体的方式告诉它会很棒,谢谢。!
As such, should I assume iPhone7,1
and iPhone7,2
are the iPhone 6 while iPhone7,3
and iPhone7.4
are the pluses? If anyone has more concrete way to tell it'd be great, thanks.!
推荐答案
第一个屏幕将是设备屏幕,请注意,之前必须添加新手机的启动图像,否则应用程序在较旧的应用程序的Zoomed模式下运行:
这是我用来检查这个的代码。 注意:这仅适用于iOS 8及更高版本:
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:
UIScreen *mainScreen = [UIScreen mainScreen];
NSLog(@"Screen bounds: %@, Screen resolution: %@, scale: %f, nativeScale: %f",
NSStringFromCGRect(mainScreen.bounds), mainScreen.coordinateSpace, mainScreen.scale, mainScreen.nativeScale);
检测iPhone 6 Plus的代码:
Code for detecting 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;
}
或
-(BOOL) iPhone6PlusUnZoomed{
if ([self iPhone6PlusDevice]){
if ([UIScreen mainScreen].bounds.size.height > 720.0) return YES; // Height is 736, but 667 when zoomed.
}
return NO;
}
注意:如果您要检查iPhone 6 Plus,请调整用户界面然后不要依赖 .nativeScale
,因为模拟器和实际设备会给出不同的结果。由于下面的评论。 Scale是一个CGFloat,因此代码不应该检查相等性,因为一些浮点值可能永远不会相等。
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.
添加后 iPhone6 和 6Plus 的启动图片,尺寸会发生变化。他们缩放旧版应用以适应屏幕。
After adding startup images for the new iPhone6 and 6Plus, the sizes change. They scale older apps to fit the screen.
iPhone 6 Plus 和 iPhone 6S Plus 的尺寸@ 3x缩放(Apple名称: Retina HD 5.5 ),坐标空间: 414 x 736 点和 1242 x 2208 像素,401 ppi,屏幕物理尺寸是2.7 x 4.8英寸或 68 x 122毫米:
Size for iPhone 6 Plus and iPhone 6S Plus with @3x scaling (Apple name: Retina HD 5.5), Coordinate space: 414 x 736 points and 1242 x 2208 pixels, 401 ppi, screen physical size is 2.7 x 4.8 in or 68 x 122 mm:
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
iPhone 6 和 iPhone 6S 的尺寸,缩放比例为@ 2x(Apple名称: Retina HD 4.7 ),坐标空间: 375 x 667 点数和 750 x 1334 像素,326 ppi,屏幕物理尺寸为2.3 x 4.1英寸或 58 x 104毫米:
Size for iPhone 6 and iPhone 6S with @2x scaling (Apple name: Retina HD 4.7), Coordinate space: 375 x 667 points and 750 x 1334 pixels, 326 ppi, screen physical size is 2.3 x 4.1 in or 58 x 104 mm:
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
用于比较的iPhone 5 为640 x 1136, iPhone 4 640 x 960。
And iPhone 5 for comparison is 640 x 1136, iPhone 4 640 x 960.
注意:上传LaunchImages否则应用程序将运行缩放,并且不会显示正确的缩放比例或屏幕尺寸。
这篇关于以点值检测iPhone 6/6 +屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!