Detecting iPhone 6/6+ screen sizes in point values

前端 未结 16 2143
独厮守ぢ
独厮守ぢ 2020-11-29 15:44

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)


        
相关标签:
16条回答
  • 2020-11-29 15:55

    this is guaranteed to compile in xcode 5 (xocde 6 at this point is still flaky, and you can't submit an ipa to itunes connect for app store approval using beta software, which xcode 6 is right now)

    the thing is xcode 5 won't recognize the nativeScale selector.. this is how you can do it in run time:

    + (BOOL)isIphone6Plus
    {
        SEL selector = NSSelectorFromString(@"nativeScale");
        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];
                NSLog(@"::: this is native scale %f", returnValue);
                return (returnValue == 3.0f);
        } else {
            // iphone 6 plus come prepackaged with iOS8.. 
            // so if the phone doesn't know what nativeScale means
            // it's not an iphone6plus phone
            return NO;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:03

    Swift 4

    if(kSize.width == 320){
                 //iphone se
    }else if(kSize.width == 375 && kSize.height == 667){
                 //iphone 7 / 8
    }else if(kSize.width == 375){
                //iphone x
    }else if(kSize.width == 414){
               //iphone 7 plus/ 8 plus
    }
    

    kSize is screen CGSize

    let kSize = UIScreen.main.bounds.size

    0 讨论(0)
  • 2020-11-29 16:04

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

    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:

    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:

    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
    
    0 讨论(0)
  • 2020-11-29 16:05

    One major thing that the above answers are leaving out is the fact that in iOS7 and below, when you check [[UIScreen mainScreen] bounds] for the screen bounds, it always lists the width and height as the same no matter what orientation the phone is in. So if it's an iPhone5 in landscape mode, it will still list the width as 320 and height as 568. In iOS8, this changed, now if that same iPhone5 is in landscape, it will list the width as 568 and the height as 320. Below are methods which account for this:

    + (BOOL) deviceHasFourInchScreen
    {
        return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:568.0];
    }
    
    + (BOOL) deviceHasFourPointSevenInchScreen
    {
        return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:667.0];
    }
    
    + (BOOL) deviceHasFivePointFiveInchScreen
    {
        return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:3.0 height:736.0];
    }
    
    + (BOOL) deviceHasScreenWithIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom scale:(CGFloat)scale height:(CGFloat)height
    {
        CGRect mainScreenBounds = [[UIScreen mainScreen] bounds];
        CGFloat mainScreenHeight;
    
        if ([OperatingSystemVersion operatingSystemVersionLessThan:@"8.0"])
        {
            mainScreenHeight = mainScreenBounds.size.height;
        }
        else
        {
            mainScreenHeight = (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) ? mainScreenBounds.size.width : mainScreenBounds.size.height;
        }
    
        if ([[UIDevice currentDevice] userInterfaceIdiom] == userInterfaceIdiom && [[UIScreen mainScreen] scale] == scale && mainScreenHeight == height)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    

    Also here are the accompanying OperatingSystem class methods:

    + (NSString *) currentOperatingSystemVersion
    {
        return [[UIDevice currentDevice] systemVersion];
    }
    + (BOOL) operatingSystemVersionLessThanOrEqualTo:(NSString *) operatingSystemVersionToCompare
    {
        return ([[self currentOperatingSystemVersion] compare: operatingSystemVersionToCompare options:NSNumericSearch] != NSOrderedDescending);    
    }
    
    0 讨论(0)
  • 2020-11-29 16:09

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

    #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    #define IS_IPHONE_6PLUS (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f)
    
    0 讨论(0)
  • 2020-11-29 16:09

    Here is the updated source code you are using this.

    iPhone 6 and iPhone 6 Plus models have been added.

    0 讨论(0)
提交回复
热议问题