Quantcast
Channel: Guilmo
Viewing all articles
Browse latest Browse all 10

How to load a different XIB for each device

$
0
0


Apple has decided to grace us with a new iPhone 5, but with the new iPhone came a new device resolution, and with a new device resolution came more work for the developers to adjust their apps to be compatible with the new screen dimension.

The new iPad Mini has the same resolution as the iPad 2, so you don’t need to worry about the Mini!

So now when loading your XIBs files you need to load three different XIBs (if your app is universal) or 2 XIBs if you’re only targeting iPhone  and iPod Touch.

The code to load your XIBs is quite simple

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

What this code actually do is:

  1. Check to see if the device is an iPhone/iPod or if it’s an iPad using the [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone
  2. Check what is the maximum device height, if it’s 568, then it’s the new iPhone 5 or iPod Touch, if not, then it’s the regular iPhone.

And that’s it. Inside the IF-ELSE statements, you load your UIViewController and do any code necessary.

Do you have any questions, comments, compliments? hit the comments below.

Thanks

.gm.


Viewing all articles
Browse latest Browse all 10

Trending Articles