Reputation: 691
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:
- (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.
Upvotes: 8
Views: 7918
Reputation: 22992
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"];
}
}
Upvotes: 3
Reputation: 3081
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:
// 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 :)
Upvotes: 5
Reputation: 52575
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.
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.
In short: check for a screen height of 480 (normal) or 568 (iPhone 5).
Upvotes: 8
Reputation: 1484
try http://github.com/erica/uidevice-extension/
[[UIDevice currentDevice] platformType] // ex: UIDevice4GiPhone
[[UIDevice currentDevice] platformString] // ex: @"iPhone 4G"
or you can just watch screenHeight like:
float screenHeight = [UIScreen mainScreen].bounds.size.height;
for the iPhone 5 height is 568
and maybe you shell to set nib if you load with an .xib like:
[[Selection alloc] initWithNibName:@"here_is_nibname" bundle:nil];
Upvotes: 5