2014-09-26 102 views
1

如果這是一個非常愚蠢的問題,請原諒我,我只是一個初學者。我想爲iPhone 6和6 Plus製作一個單獨的界面,但我不知道如何才能做到這一點。這是一個非常簡單的應用程序,只是爲了讓自己更熟悉該語言等。我想知道是否有一種簡單的方法可以告訴用戶是否使用6或6加上爲他們創建單獨的界面。謝謝:)如何判斷用戶是否使用iPhone 6或6+

+5

使用一個情節串連圖板爲所有iPhone。正確使用自動佈局和約束來將事物放在正確的位置和大小。 – rmaddy 2014-09-26 22:12:51

+0

這篇文章可能也會幫助你:http://stackoverflow.com/questions/25756087/detecting-iphone-6-6-screen-sizes-in-point-values – 2014-09-26 22:16:52

回答

-1

在prefix.pch將低於線

#define iPhoneVersion ([[UIScreen mainScreen] bounds].size.height == 568 ? 5 : ([[UIScreen mainScreen] bounds].size.height == 480 ? 4 : ([[UIScreen mainScreen] bounds].size.height == 667 ? 6 : ([[UIScreen mainScreen] bounds].size.height == 736 ? 61 : 999)))) 

現在在編程,你可以說...

if (iPhoneVersion==4) { 
    NSLog("This is 3.5 inch iPhone - iPhone 4s or below"); 
} else if (iPhoneVersion==5) { 
    NSLog("This is 4 inch iPhone - iPhone 5 family"); 
} else if (iPhoneVersion==6) { 
    NSLog("This is 4.7 inch iPhone - iPhone 6"); 
} else if (iPhoneVersion==61) { 
    NSLog("This is 5.5 inch iPhone - iPhone 6 Plus.. The BIGGER"); 
} else { 
    NSLog("This is iPad"); 
} 

然而我會說所有使用一個故事板學習自動佈局的iPhone。

+2

使用'switch'語句。否則,對於iPad而言,您最終會對這一大堆代碼進行四次評估(每個if語句一次)。在iOS 8環境下,代碼將失敗。 – rmaddy 2014-09-26 23:32:03

+0

這是一個相當愚蠢的做法。當下一個iPhone發佈並且代碼錯誤地認爲它是iPad時,它會中斷。 – gnasher729 2014-09-27 12:57:36

+0

@ gnasher729:iPad尺寸的iPhone?偉大的想法...享受編程... – 2014-09-27 12:58:55

2

使用單個故事板。從raywanderlich瞭解autolayout,它認爲它是一個非常好的資源。

此外,現在利用在xcode 6中爲ios 8引入的大小類別。這稱爲自適應佈局

因此,不要分別爲3.5,4,4.7,5.5英寸設備分別製作上述四個視圖,然後考慮將各自的風景模式組合起來,只需使用自動佈局(if可能,強烈推薦),並放鬆任何其他可能會出現的設備尺寸。

0

檢測iPhone版請使用下面的代碼。

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
     if ([[UIScreen mainScreen] scale] == 2.0) { 

      if([UIScreen mainScreen].bounds.size.height == 667){ 
      // iPhone retina-4.7 inch(iPhone 6) 
      } 
      else if([UIScreen mainScreen].bounds.size.height == 568){ 
      // iPhone retina-4 inch(iPhone 5 or 5s) 
      } 
      else{ 
      // iPhone retina-3.5 inch(iPhone 4s) 
      } 
     } 
     else if ([[UIScreen mainScreen] scale] == 3.0) 
     { 
      if([UIScreen mainScreen].bounds.size.height == 736.0){ 
       //iPhone retina-5.5 inch screen(iPhone 6 plus) 
      } 
     } 
} 

借鑑Here

0

寫這段代碼.PCH文件

#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0) 
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0) 

和wwhenever你在任何控制器需要你簡單地檢查車況。

0

下面是做你所問的方法。要注意的一件事是在iOS7及更低版本中,當您檢查[[UIScreen mainScreen] bounds]的屏幕邊界時,無論手機處於何種方向,它總是列出寬度和高度。因此,如果是處於橫向模式的iPhone5,它仍然會列出寬度爲320,高度爲568.在iOS8中,這種情況發生了變化,現在如果同一個iPhone5處於橫向狀態,它將列出寬度爲568,高度爲320.下面是解釋此方法的方法:

+ (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; 
    } 
} 

而且,這裏是伴隨OperatingSystem的類方法:

+ (NSString *) currentOperatingSystemVersion 
{ 
    return [[UIDevice currentDevice] systemVersion]; 
} 
+ (BOOL) operatingSystemVersionLessThanOrEqualTo:(NSString *) operatingSystemVersionToCompare 
{ 
    return ([[self currentOperatingSystemVersion] compare: operatingSystemVersionToCompare options:NSNumericSearch] != NSOrderedDescending);  
} 
相關問題