Altenative [UIScreen mainScreen].nativeScale for iOS 7 suppo



有人知道下面代码的替代方案吗?我想在我的应用程序中支持iOS 7及以下版本,nativeScale在这些固件上不起作用。我在网上找不到解决方案,所以我在这里问。(通常让屏幕边界检查568的高度对iPhone 6+不起作用)

我所指的代码:

CGRect screenBounds = [[UIScreen mainScreen] bounds];
if ([UIScreen mainScreen].nativeScale > 2.1) {
//6 plus
} else if (screenBounds.size.height == 568) {
//4 inch / iPhone 6 code
} else {
//3.5 inch code
}

提前感谢

所以我所做的是:首先,我有以下顺序的方法:

if ([UIScreen mainScreen].nativeScale > 2.1) {
    //6 plus
} else if (screenBounds.size.height == 568) {
    //4 inch code
} else {
//3.5 inch code
}

然后我想,一旦计算机找到一个真的if else语句,它就会停止运行,我就把顺序重新排列为:

if (screenBounds.size.height == 480) {
//3.5 inch code
} else if ([UIScreen mainScreen].nativeScale > 2.1) {
    //6 plus
} else if (screenBounds.size.height == 568) {
    //4 inch code
}

这支持iOS 7或更低版本的iPhone 4S。在iPhone 5/5S上,它仍然会崩溃。这就是为什么我最终将其更改为以下内容:

if ([[UIScreen mainScreen] respondsToSelector:@selector(nativeScale)]) {
//checks if device is running on iOS 8, skips if not
    NSLog(@"iOS 8 device");
    if (screenBounds.size.height == 480) {
        //3.5 inch code
        NSLog(@"iPhone 4S detected");
    } else if ([UIScreen mainScreen].nativeScale > 2.1) {
        //6 plus
        NSLog(@"iPhone 6 plus detected");
    } else if (screenBounds.size.height == 568) {
        //4 inch code
        NSLog(@"iPhone 5 / 5S / 6 detected");
    }
} else if (screenBounds.size.height == 480) {
    //checks if device is tunning iOS 7 or below if not iOS 8
    NSLog(@"iOS 7- device");
    NSLog(@"iPhone 4S detected");
//3.5 inch code
} else if (screenBounds.size.height == 568) {
    NSLog(@"iOS 7- device");
    NSLog(@"iPhone 5 / 5S / 6 detected");
    //4 inch code
}

它现在应该可以在任何iOS 7和iOS 8设备上完全工作!

最新更新