xcode 为 iPhone 5 加载不同的 xib
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12688226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading a different xib for the iPhone 5
提问by Dale Townsend
My apps has extra functionality for the iPhone 5, and I've created a separate class with an .xib for it. I would like to detect the screen height (unless it's possible to get the device ID/model) and load a different view controller accordingly. I have tried this:
我的应用程序为 iPhone 5 提供了额外的功能,我为它创建了一个带有 .xib 的单独类。我想检测屏幕高度(除非可以获取设备 ID/型号)并相应地加载不同的视图控制器。我试过这个:
- (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 are two different classes, each with a different xib for the user interface.
Selection 和 Selection_5 是两个不同的类,每个类都有不同的用户界面 xib。
采纳答案by Evgeniy S
try http://github.com/erica/uidevice-extension/
试试http://github.com/erica/uidevice-extension/
[[UIDevice currentDevice] platformType] // ex: UIDevice4GiPhone
[[UIDevice currentDevice] platformString] // ex: @"iPhone 4G"
or you can just watch screenHeight like:
或者你可以只看 screenHeight 像:
float screenHeight = [UIScreen mainScreen].bounds.size.height;
for the iPhone 5 height is 568
iPhone 5 的高度为 568
and maybe you shell to set nib if you load with an .xib like:
如果你加载一个 .xib,也许你可以设置 nib:
[[Selection alloc] initWithNibName:@"here_is_nibname" bundle:nil];
回答by Stephen Darlington
Firstly, you don't want to check by device type. What would happen on the new iPod touches (which have the same size screen) or next years iPhone.
首先,您不想按设备类型进行检查。在新的 iPod touch(具有相同尺寸的屏幕)或明年的 iPhone 上会发生什么。
But I think the problem here is that you're checking for the screen size based on the actualy number of pixels which -- bizarrely -- is not what you want. Remember that on a Retina screen everything is "doubled." In the UI you (mostly) use the "normal" size for everything which, in this case, is half the number of pixels.
但我认为这里的问题是您正在根据实际像素数检查屏幕尺寸 - 奇怪的是 - 这不是您想要的。请记住,在 Retina 屏幕上,一切都是“翻倍的”。在 UI 中,您(大多数情况下)对所有内容使用“正常”大小,在这种情况下,它是像素数的一半。
In short: check for a screen height of 480 (normal) or 568 (iPhone 5).
简而言之:检查屏幕高度是否为 480(正常)或 568(iPhone 5)。
回答by gmogames
In my app I have to load a .XIB file for iPhone, iPhone5/iPod Touch and iPad, for that, this is the code I use:
在我的应用程序中,我必须为 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];
...
}
Hope it helps someone that needs :)
希望它可以帮助需要的人:)
回答by hfossli
If you've got this naming convention
如果你有这个命名约定
VGArticlePage~ipad.xib
VGArticlePage~iphone.xib
VGArticlePage~iphone_ext.xib
Then you can do like this
然后你可以这样做
#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"];
}
}

