Detecting iPhone 6/6+ screen sizes in point values

前端 未结 16 2273
独厮守ぢ
独厮守ぢ 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:54

    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):

    -(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.

提交回复
热议问题