2014-10-18 166 views
0

有没有人知道以下代码的替代?我想在我的应用程序中支持iOS 7及以下版本,并且.nativeScale不适用于这些固件。我无法在互联网上找到解决方案,这就是我在这里问的原因。 (常让screenbounds检查的568高度为iPhone不起作用6+)支持iOS 7支持的[UIScreen mainScreen] .nativeScale?

守则我指的是:

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 

} 

在此先感谢

+0

你的目标是?没有iPhone 6/6 +将运行iOS 7.为什么你声称568的高度等同于iPhone 6?这是iPhone 5的高度,而不是iPhone 6(除非您的应用程序正在缩放)。 – rmaddy 2014-10-18 05:21:23

+0

我的目标是在可能的情况下支持iOS 7和6。现在我的应用程序崩溃了(在我的iPhone 4S - 7.1.1),因为.nativeScale在iOS 8下不兼容。 我的意思正是你说的,568不适用于iPhone 6+,这就是为什么我必须实现第二种方法(.nativeScale) – LinusGeffarth 2014-10-18 05:24:05

+1

如何:'if([[UIScreen mainScreen] respondsToSelector:@selector(nativeScale)]){.. ios 8 code ..}' – 2014-10-18 05:32:19

回答

4

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

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 4或以下的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设备上完全工作!