2012-10-02 43 views
8

我的应用程序为iPhone 5提供了额外的功能,并且为它创建了一个带有.xib的独立类。我想检测屏幕高度(除非可以获取设备ID /型号)并相应地加载不同的视图控制器。我试过这个:为iPhone加载不同的xib 5

- (IBAction)select:(id)sender { 

CGRect screenRect = [[UIScreen mainScreen] bounds]; 
CGFloat screenWidth = screenRect.size.width; 
CGFloat screenHeight = screenRect.size.height; 

if (screenHeight == 960) { 

Selection *selectView =[[Selection alloc] initWithNibName:nil bundle:nil]; 
selectView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
[self presentModalViewController:selectView animated:YES]; 

} 

else { 

    Selection_5 *selectView =[[Selection_5 alloc] initWithNibName:nil bundle:nil]; 
    selectView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
    [self presentModalViewController:selectView animated:YES]; 

} 

} 

Selection and Selection_5是两个不同的类,每个类都有不同的xib用于用户界面。

+0

是的,它工作。我用浮screenHeight = [UIScreen mainScreen] .bounds.size.height –

+0

这是帮你解决这个问题 http://stackoverflow.com/questions/13275144/how-to-make-xib-compatible-与iphone5和iPhone4设备/ 13283851#13283851 –

回答

5

尝试http://github.com/erica/uidevice-extension/

[[UIDevice currentDevice] platformType] // ex: UIDevice4GiPhone 
[[UIDevice currentDevice] platformString] // ex: @"iPhone 4G" 

,或者你可以只是看screenHeight这样的:

float screenHeight = [UIScreen mainScreen].bounds.size.height; 

对于iPhone 5的高度是568

,也许你程序设置笔尖如果您加载.xib:

[[Selection alloc] initWithNibName:@"here_is_nibname" bundle:nil]; 
+0

谢谢,使用float screenHeight = [UIScreen mainScreen] .bounds.size.height工作。 –

+0

在横向模式我无法得到设备的确切宽度,请任何一个共享如何得到... – Karthik

+0

在景观,你需要比较不是与高度568,但与宽度568. –

8

首先,您不想通过设备类型进行检查。新的iPod触摸(具有相同尺寸的屏幕)或下一年iPhone会发生什么情况。

但我认为这里的问题是,你正在检查屏幕大小的像素的实际数量 - 奇怪 - 不是你想要的。请记住,在Retina屏幕上,所有内容都“加倍”。在用户界面中,您(大多数情况下)使用“正常”尺寸的任何东西,在这种情况下,它是像素数量的一半。

总之:检查屏幕高度480(普通)或568(iPhone 5)。

+0

啊,我终于得到什么568是参考!我使用它为4“设备加载不同的xib –

5

在我的应用我需要加载的iPhone,iPhone5的/ iPod Touch和iPad上的.xib文件,对于这一点,这是我使用的代码:

// If Iphone/iPod Touch 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
    // If iPhone 5 or new iPod Touch 
    if([UIScreen mainScreen].bounds.size.height == 568){ 
    VCDadosViewController *extratoVC = [[VCDadosViewController alloc] initWithNibName:@"VCDadosViewControllerExt" bundle:nil]; 
    ... 
    } else{ 
    // Regular iPhone 
    VCDadosViewController *extratoVC = [[VCDadosViewController alloc] initWithNibName:@"VCDadosViewController" bundle:nil]; 
    ...   
    } 
// If iPad 
} else { 
    VCDadosViewController *extratoVC = [[VCDadosViewController alloc] initWithNibName:@"VCDadosViewControllerPad" bundle:nil]; 
    ... 
} 

希望它可以帮助别人,需要:)

3

如果你有这个命名约定

VGArticlePage~ipad.xib 
VGArticlePage~iphone.xib 
VGArticlePage~iphone_ext.xib 

然后你就可以像这样

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f) 

- (NSString *)nibNameForClass:(Class)class 
{ 
    if(IS_IPHONE && IS_IPHONE_5) 
    { 
     return [NSString stringWithFormat:@"%@%@", NSStringFromClass(class), @"~iphone_ext"]; 
    } 
    else if(IS_IPHONE) 
    { 
     return [NSString stringWithFormat:@"%@%@", NSStringFromClass(class), @"~iphone"]; 
    } 
    else 
    { 
     return [NSString stringWithFormat:@"%@%@", NSStringFromClass(class), @"~ipad"]; 
    } 
} 
+0

如果nibNameForClass方法作为静态函数放置在Util类中,它可以在应用程序中的任何地方使用,我更喜欢这个方法而不是gmogames的解决方案。 – Zuppa

相关问题