为iPhone 5加载不同的xib
我的应用程序为iPhone5提供了额外的功能,我还为它创建了一个单独的类,其中包含一个.xib。我想检测屏幕高度(除非有可能获取设备ID/型号),并相应地加载不同的视图控制器。我试过这个:为iPhone 5加载不同的xib,iphone,objective-c,ios,xcode,ipad,Iphone,Objective C,Ios,Xcode,Ipad,我的应用程序为iPhone5提供了额外的功能,我还为它创建了一个单独的类,其中包含一个.xib。我想检测屏幕高度(除非有可能获取设备ID/型号),并相应地加载不同的视图控制器。我试过这个: - (IBAction)select:(id)sender { CGRect screenRect = [[UIScreen mainScreen] bounds]; CGFloat screenWidth = screenRect.size.width; CGFloat screenHeight = sc
- (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和Selection_5是两个不同的类,每个类的用户界面都有不同的xib。试试看
或者,您可以只观看屏幕高度,如:
float screenHeight = [UIScreen mainScreen].bounds.size.height;
iPhone 5的高度是568
如果使用.xib加载,可能需要设置nib,如:
[[Selection alloc] initWithNibName:@"here_is_nibname" bundle:nil];
首先,您不想按设备类型进行检查。新的iPodtouch(屏幕大小相同)或未来几年的iPhone会发生什么 但我认为这里的问题是,你在根据实际像素数检查屏幕大小,奇怪的是,这不是你想要的。请记住,在视网膜屏幕上,所有内容都是“翻倍”的。在UI中,您(大部分)使用“正常”大小,在本例中,这是像素数的一半
简而言之:检查屏幕高度是否为480(正常)或568(iPhone 5)。在我的应用程序中,我必须为iPhone、iPhone 5/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];
...
}
希望它能帮助有需要的人:)如果你有这样的命名约定
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"];
}
}
谢谢,使用float Screen height=[UIScreen mainScreen].bounds.size.height有效。在横向模式下,我无法获得设备的确切宽度,请任何人分享如何获得…在横向模式下,您需要比较的不是高度568,而是宽度568。啊,我终于得到了568的参考值!我用它来为4英寸设备加载不同的xib。是的,它起作用了。我使用了浮动屏幕高度=[UIScreen mainScreen].bounds.size.Heights这有助于解决此问题如果将nibNameForClass方法作为Util类中的静态函数放置,则它可以在应用程序中的任何位置使用。我更喜欢这种方法,而不是gmogames的解决方案。
#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"];
}
}