2014-10-18 194 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設備上完全工作!