2014-10-16 90 views
1

我使用的代碼顯示了一個特定的xib文件,具體取決於設備,目前有3個xibs,一個用於iPhone4,一個用於iPhone5,另一個用於iPad。定義大於iPhone 6的屏幕尺寸?

自iOS8/iPhone 6以來,由於xib不知道要載入什麼內容,所以該應用程序無法在高於5s的任何內容上運行;

- (IBAction)btnTapForStart:(id)sender { 

    fromOtherClass=YES; 

    if([[UIScreen mainScreen]bounds].size.height == 568) 
    { 
     PlayMusicViewController *pmvc = [[PlayMusicViewController alloc]initWithNibName:@"XibForIPhone5" bundle:[NSBundle mainBundle]]; 

     [self.navigationController pushViewController:pmvc animated:YES]; 

    } 
    else 
    { 
     PlayMusicViewController *pmvc = [[PlayMusicViewController alloc]init]; 

     [self.navigationController pushViewController:pmvc animated:YES]; 
    } 

    [appObject.audioPlayer stop]; 
} 

如果我改變的568更高,將工作的價值,但有沒有辦法結合起來,例如,高度爲568或iPhone6大小或iPhone 6加上大小等?

+0

爲什麼你有多個xibs?只需使用一個用於所有設備(可能是iPad的第二個)。使用自動佈局和約束來正確地確定所有尺寸和位置。 – rmaddy 2014-10-16 15:45:52

+1

我最初並沒有編寫代碼,只是希望解決它,所以它適用於那些有應用程序並使用iPhone 6/6 Plus的人,我可以擔心之後改進代碼,但這是一個更緊急的解決方法。 .. 感謝誰標記了這個答案也... – user3355723 2014-10-16 15:52:17

+0

那麼你的問題是什麼?你想爲iPhone 6使用iPhone 5 xib嗎?還是你有另一個特定於iPhone 6的xib? – rmaddy 2014-10-16 15:53:58

回答

1

您可能應該使用像Maddy說的自動佈局,但是如果您想讓代碼適用於特定設備,那麼您可能需要實現此實用程序方法。然後,您可以根據需要創建儘可能多的xib,並將它們定位到特定設備。例如

if ([[Utilities deviceType] isEqualToString:@"iPhone Retina4"] || [[Utilities deviceType] isEqualToString:@"iPhone Retina35"]) { 
    -- do something specific for these phones 
} 

我把這個方法放在一個Utilities類中。

+ (NSString *)deviceType { 
    NSString *device = nil; 
    CGSize screenSize = [[UIScreen mainScreen] bounds].size; 
    CGFloat deviceScale = [UIScreen mainScreen].scale; 

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { 
     device = @"iPhone Classic"; // Just in case it doesn't make it through the conditionals 
     // Classic has a resolution of 480 × 320 
     if((screenSize.height == 480 || screenSize.width == 480) && deviceScale == 1.0f) { 
      device = @"iPhone Classic"; 
     // Retina has a resolution of 960 × 640 
     } else if((screenSize.height == 480 || screenSize.width == 480) && deviceScale == 2.0f) { 
      device = @"iPhone Retina35"; 
     // Retina 4" has a resolution of 1136 x 640 
     } else if (screenSize.height == 568 || screenSize.width == 568) { 
      device = @"iPhone Retina4"; 
     // iPhone 6 has a resolution of 1334 by 750 
     } else if (screenSize.height == 667 || screenSize.width == 667) { 
      device = @"iPhone 6"; 
     // iPhone 6 Plus has an actual size of 2208 × 1242 and resolution of 1920 by 1080 
     // Reported size is 736 x 414 
     } else if (screenSize.height == 736 || screenSize.width == 736) { 
      device = @"iPhone 6 Plus"; 
     } 

    } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
     device = @"iPad Classic"; // Just in case it doesn't make it through the conditionals 
     if(deviceScale == 1.0f) { 
      device = @"iPad Classic"; 
     } else if (deviceScale == 2.0f) { 
      device = @"iPad Retina"; 
     } 
    } 
    //NSLog(@"The device is %@ scale is %f and the height is %f and width is %f", device, deviceScale, screenSize.height, screenSize.width); 

    return device; 
} 
+0

爲什麼你會在這裏使用字符串?枚舉會做。 – StilesCrisis 2014-10-16 16:01:14

+0

@StilesCrisis,我沒有正式的語言培訓,所以我的知識庫中有很多漏洞。我對枚舉的瞭解以及如何使用它們是最小的。 – JScarry 2014-10-17 14:39:18

0

我做了一些關於枚舉的研究,他們很好。它們使代碼更具可讀性,但大多數情況下它們允許編譯器幫助您鍵入並捕獲錯誤。 Xcode會自動完成你的設備類型,並會給你錯誤:如果你嘗試使用未定義的值,則使用未聲明的標識符。這裏的代碼被重寫爲一個枚舉。我用LF前綴了這些值,但是您應該使用適合您項目的內容。

這是我的頭文件

// Devices as of Fall 2014 
typedef NS_ENUM(NSInteger, LFdeviceType) { 
    LFDeviceTypePhoneClassic, 
    LFDeviceTypePhoneRetina3_5, 
    LFDeviceTypePhoneRetina4, 
    LFDeviceTypePhone6, 
    LFDeviceTypePhone6Plus, 
    LFDeviceTypePadClassic, 
    LFDeviceTypePadRetina, 
}; 

而且這是在我的.m文件。

m+ (NSInteger)deviceType { 
    CGSize screenSize = [[UIScreen mainScreen] bounds].size; 
    CGFloat deviceScale = [UIScreen mainScreen].scale; 
    LFdeviceType device = LFDeviceTypePhoneClassic; 

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { 
     device = LFDeviceTypePhoneClassic; // Just in case it doesn't make it through the conditionals 
     // Classic has a resolution of 480 × 320 
     if((screenSize.height == 480 || screenSize.width == 480) && deviceScale == 1.0f) { 
      device = LFDeviceTypePhoneClassic; 
     // Retina has a resolution of 960 × 640 
     } else if((screenSize.height == 480 || screenSize.width == 480) && deviceScale == 2.0f) { 
      device = LFDeviceTypePhoneRetina3_5; 
     // Retina 4" has a resolution of 1136 x 640 
     } else if (screenSize.height == 568 || screenSize.width == 568) { 
      device = LFDeviceTypePhoneRetina4; 
     // iPhone 6 has a resolution of 1334 by 750 
     } else if (screenSize.height == 667 || screenSize.width == 667) { 
      device = LFDeviceTypePhone6; 
     // iPhone 6 Plus has an actual size of 2208 × 1242 and resolution of 1920 by 1080 
     // Reported size is 736 x 414 
     } else if (screenSize.height == 736 || screenSize.width == 736) { 
      device = LFDeviceTypePhone6Plus; 
     } 

    } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
     device = LFDeviceTypePadClassic; // Just in case it doesn't make it through the conditionals 
     if(deviceScale == 1.0f) { 
      device = LFDeviceTypePadClassic; 
     } else if (deviceScale == 2.0f) { 
      device = LFDeviceTypePadRetina; 
     } 
    } 
    //NSLog(@"The device is %@ scale is %f and the height is %f and width is %f", device, deviceScale, screenSize.height, screenSize.width); 

    return device; 
} 

這樣稱呼它:

if (( [Utilities deviceType] == LFDeviceTypePhoneClassic 
     || [Utilities deviceType] == LFDeviceTypePhoneRetina3_5) && 
     numberOfFoilsOnScreen > 7) { 
     numberOfFoilsOnScreen = 7; 
}