Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Is it possible to create 2 storyboards?

Hey guys I have a question regarding iOS iPhone projects.

Say I want my project to be compatible with both Retina Display screens of 640x960 px, and the new iPhone 5 screen resolution.

If I'm using storyboards, the "application:didFinishLaunchingWithOptions:" method simply returns YES, and there is no code inside it. I know that we specify visually which is the Main Storyboard to be loaded.

My question is:

Is it possible to create 2 separate storyboards for iPhone, and load the corresponding one (based on the iPhone/iPod Touch device running on) some way?

Thanks in advance

4 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

@pavel You can have multiple storyboards but a better solution is to use Auto Layout with constraints.

Thank for your reply, Amit.

I know it's possible to create multiple storyboards. But I want to make 2 storyboards: one with auto-layout and bigger screen sizes for iPhone 5, and the other - for iPhone 4 with Retina Display and iOS 5, and without auto-layout.

But how to specify which one to load?

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

First you will need a macro to determine whether the device is an iPhone 5

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

Next in your didFinishLaunchingWithOptions method you can do a test and load the relevant storyboard

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{  
    self.window = [[UIWindow alloc] init];

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController* initialViewController = [storyboard instantiateInitialViewController];

    if ( IS_IPHONE_5 ){
        storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        initialViewController = [storyboard instantiateInitialViewController];
    } else {
        storyboard = [UIStoryboard storyboardWithName:@"OtherStoryboard" bundle:nil];
        initialViewController = [storyboard instantiateInitialViewController];        
    }

    self.window.rootViewController = initialViewController;
    [self.window makeKeyAndVisible];

}

Note: make sure to remove the MainStoryboard from your project summary

Thanks a lot Amit. It's exactly what I was looking for.