How To Load Different XIBs Based On The Device Type?

mainWhat’s with all the different sizes of the iOS devices? I mean, sure we have autolayout feature that takes care of things in general. But if you want real aesthetic perfection, then you are better off having a separate XIB for each screen size. If you create a universal app, Xcode automatically creates two separate XIBs for iPhone and iPad. But if you want to differentiate between the 3.5 inch screen and the 4 inch screen on the iPhone, you should create a new XIB and add it to your project. You should of course name them accordingly. You can then load the XIBs by checking for the device type as given below.  

Inside the AppDelegate.m file, make the following changes to load different XIBs based on the device type:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainSc                                                    reen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInte                                                  rfaceIdiomPhone) 
    {
        if ([[UIScreen mainScreen] bounds].size.height == 568)
        {
            self.viewController = [[MyViewController alloc] initWit                                   hNibName:@"MyViewController_iPho                                   ne4.0" bundle:nil];
        }
        else
        {
            self.viewController = [[MyViewController alloc] initWit                                    hNibName:@"MyViewController_iPh                                    one3.5" bundle:nil];
        }
    else 
    {
        self.viewController = [[T2ViewController alloc] initWithNib                         Name:@"T2ViewController_iPad" bundle:nil];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

You can in fact use this condition anywhere in your project when you go back and forth between different XIBs. It’s a really nice thing to have when you are building a universal app but don’t want to deal with all the misalignments of your objects.

————————————————————————————————-

One thought on “How To Load Different XIBs Based On The Device Type?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s