0
votes

So I am making an iPhone app and I would like to have a different storyboard for each screen size. (1 for the iPhone 6 and 1 for the 6 plus and 1 for the 4 and 1 for the 5) I know that I can use size classes but I have my reasons for wanting to use multiple storyboards. Anyway In order to do this I put the following code in my app delegate...

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

UIStoryboard *storyboard = nil;
if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPad) {
    storyboard = [UIStoryboard storyboardWithName:@"3inchstoryboard" bundle:nil];//iPad
} else {
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    if (screenSize.height == 480){
        storyboard = [UIStoryboard storyboardWithName:@"3inchstoryboard" bundle:nil];//iPhone 3.5inch
    } else
        if (screenSize.height == 568){
            storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];//iPhone 4inch
        }
       else
        { if (screenSize.height == 667){
            //storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];//iPhone 4.7inch
       } else
            if (screenSize.height == 736){
                storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];//iPhone 5.5inch
           } else
                //default storyboard
                storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        }
}
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];

This code does not work. Every time I run it, no matter the screen size, the device always goes to the default at the bottom of the code. It worked on my iPhone 5 only and my iPhone 5 is running iOS 7. I have definitely deleted the bar in the plist for the default interface but it still will not work. So what can I do to make this work! I would really appreciate your help!

1
Yeah this is my real code, idk y u don't think it is.msweet168
I have it in my app did finish launchingmsweet168
sorry about that, that was supposed to be commented out, i edited itmsweet168
I recopied the code and edited the question by deleting the old an putting back the newmsweet168

1 Answers

0
votes

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.