Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a different xib for the iPhone 5

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.

like image 942
Dale Townsend Avatar asked Oct 02 '12 09:10

Dale Townsend


4 Answers

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).

like image 182
Stephen Darlington Avatar answered Nov 15 '22 21:11

Stephen Darlington


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];
like image 41
Evgeniy S Avatar answered Nov 15 '22 19:11

Evgeniy S


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 :)

like image 33
gmogames Avatar answered Nov 15 '22 19:11

gmogames


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"];
    }
}
like image 41
hfossli Avatar answered Nov 15 '22 19:11

hfossli