How To Load XIBs

Published on February 18th, 2013 | by Guilherme Mogames

0

How to load a different XIB for each device


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.

Tags: , , , ,


About the Author

Founder of the Guilmo.com website and freelance iOS developer for over 2 years. Creator of one of the most used financial app in Brazil and also creator of the iPhone game FlyingMoo. Over 10 years of experience with IT and over 4 years as a Scrum Master for a large multinational IT company.



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by sweet Captcha

Back to Top ↑