How to Load Multiple Storyboard Files Depending on iOS Version? (5 and 6)

How to load multiple storyboard files depending on iOS version? (5 and 6)

In your app delegate, you should find code like this:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil];
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];

This is where you can add some code to choose among the several storyboards that you have depending on the version of iOS:

UIStoryboard *storyboard;
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if (...) {
storyboard = [UIStoryboard storyboardWithName:@"MyStoryboard-v5" bundle:nil];
} else {
storyboard = [UIStoryboard storyboardWithName:@"MyStoryboard-v6" bundle:nil];
}
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];

Use different Storyboards for different iOS versions

Based on your error, it seems that you're targeting for a iOS version < 6. Probably you're using the Main Storyboard setting in the Target summary settings. As you can see here, he does exactly what you want to achieve.

Otherwise you can use two target for different versions.

Xcode 6 using multiple storyboards

I figured out an answer myself. The code is slightly different but it works very well. As said above by Matt, it's important to put NSLogs in your code so you know what is running and what is not. The first thing is I was using this code for a landscape app. There is a simple fix for this. You have to change the word Height to Width. The other thing is this will not work for the iPhone 6 or 6 plus if you do not have a launch image file. Not just a regular xcasset you also have to have a xib as a launch image which was added in Xcode 6. The code is below, please just note that in the #define it says height, if you are using this in a LANDSCAPE app change hight to width.

... This goes right under where it says "#import AppDelegate.h"

#define IS_IPHONE_4 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)480) <     DBL_EPSILON)
#define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSILON)
#define IS_IPHONE_6 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)667) < DBL_EPSILON)
#define IS_IPHONE_6_PLUS (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)736) < DBL_EPSILON)

... This goes in your "view did finish launching with options"

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

if (IS_IPHONE_4) {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhone4board" bundle:[NSBundle mainBundle]];
UIViewController *vc =[storyboard instantiateInitialViewController];
self.window.rootViewController = vc;
NSLog(@"iPhone 4 storyboard loaded up.");
}
else if(IS_IPHONE_6) {
UIStoryboard *storyboard1 = [UIStoryboard storyboardWithName:@"iPhone6board" bundle:[NSBundle mainBundle]];
UIViewController *vc1 =[storyboard1 instantiateInitialViewController];
self.window.rootViewController = vc1;
NSLog(@"iPhone 6 storyboard loaded up.");
}
else if(IS_IPHONE_5) {
UIStoryboard *storyboard1 = [UIStoryboard storyboardWithName:@"iPhone5board" bundle:[NSBundle mainBundle]];
UIViewController *vc2 =[storyboard1 instantiateInitialViewController];
self.window.rootViewController = vc2;
NSLog(@"iPhone 5 storyboard loaded up.");
}

[self.window makeKeyAndVisible];

...If you are using a landscape app, you will not be able to rotate the screen from landscape left to landscape right and vice versa.

If you want to be able to do that you can REMOVE this line...
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

If your code does not work correctly after removing that, put it back in and just don't have it rotate.

That code works and if you have any problems with it leave a comment. There is no need to delete anything from your plist or general page.

Also if you want to add a six plus line, just copy and paste one of the other if statements and change it to #define IS_IPHONE_6_PLUS. Just make sure you change it from vc1 2 or just vc to vc3 in both spots.

How to call different Storyboards via Swift for iOS?

I guess you want to open a view? If so this code will do the job:

var mainView: UIStoryboard!
mainView = UIStoryboard(name: "vcLogin", bundle: nil)
let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("iPhone5") as UIViewController
self.window!.rootViewController = viewcontroller

It will open the view controller with id: yourViewControllerId

You need to give your viewcontroller an identifier.
You do that by highlighting your view controller and then give it a identifier:
You then put your identifier in StoryBoard ID.
Sample Image

So for you it will be:

if screenHeight == 480 {
deviceFamily = "iPhoneOriginal"
// Load Storyboard with name: iPhone4
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "vcLogin", bundle: nil)
let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("iPhone4") as UIViewController
self.window!.rootViewController = viewcontroller
}

Different storyboards for different devices in Xcode 6

At Xcode 5 even using universal apps, i have two storyboards, one for iPad and another for iPhone. But now, in Xcode 6, theres just one storyboard for universal apps. I want to have two storyboards again, but I didn't find how to do this.

Do it the same way you were doing it in Xcode 5. No law says you have to have just one storyboard in a universal app in Xcode 6. The old way works just fine. I have apps that still do this (recompiled in Xcode 6 for iOS 8); it works.

However, something odd is undoubtedly going on. Look at this screen shot. It shows two projects open simultaneously in Xcode 6. One shows the iPhone / iPad buttons for switching between settings within the Universal setting; the other doesn't. I can't find a difference between the two projects that causes this differing interface.

Sample Image

How to switch to different Storyboard for iPhone 5?

I was looking for the same answer couple of weeks ago here's my solution hope helps..

-(void)initializeStoryBoardBasedOnScreenSize {

if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{ // The iOS device = iPhone or iPod Touch

CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;

if (iOSDeviceScreenSize.height == 480)
{ // iPhone 3GS, 4, and 4S and iPod Touch 3rd and 4th generation: 3.5 inch screen (diagonally measured)

// Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone35
UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:@"Storyboard_iPhone35" bundle:nil];

// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iPhone35Storyboard instantiateInitialViewController];

// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Set the initial view controller to be the root view controller of the window object
self.window.rootViewController = initialViewController;

// Set the window object to be the key window and show it
[self.window makeKeyAndVisible];
}

if (iOSDeviceScreenSize.height == 568)
{ // iPhone 5 and iPod Touch 5th generation: 4 inch screen (diagonally measured)

// Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"Storyboard_iPhone4" bundle:nil];

// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iPhone4Storyboard instantiateInitialViewController];

// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Set the initial view controller to be the root view controller of the window object
self.window.rootViewController = initialViewController;

// Set the window object to be the key window and show it
[self.window makeKeyAndVisible];
}

} else if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)

{ // The iOS device = iPad

UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;

}
}

Call this method under AppDelegate ddiFinishLaunchingWithOptions: method
And also don't forget the name your storyboards properly

Hope helps...

Using multiple Storyboards in Xcode

Find your info.plist file in your project and remove the line called "Main storyboard file base name". Sample Image

Also, you forgot to create the window (credits to rdelmar).

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

I strongly advise you to look into Auto Layout and Size Classes. Using these two, you can support all screen sizes within one single Storyboard. It's a bit hard to get in the beginning, but it'll definitely be worth it in the long run.

There's a video about this from the most recent WWDC called Building Adaptive Apps with UIKit.

Selecting different storyboards based on device type

I am not aware of any automatic selection of storyboards based on their filename suffix. You can use userInterfaceIdiom to select for iPad vs iPhone:

if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPad) {
UIStoryboard *storyboard =
[UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
} else {
[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
}

But if you are doing this to start up with a specifc view controller, all you need to do is drag the 'start' arrow to your preferred view controller in the storyboard

Or - select the view controller in the storyboard, go to the attributes instpector and tick isInitialViewController



Related Topics



Leave a reply



Submit