el-flor
el-flor

Reputation: 1476

iOS - load a different storyboard for iPhone 4S not working

I more or less finished my whole application and obviously just realised that the layout doesn't fit on the iPhone 4S only. I could start using Auto-layout but it has messed up my projects a lot, so I thought about another method: loading another Storyboard for the iPhone 4s. I found the following code on internet which seems to work for everyone:

- (UIStoryboard *)grabStoryboard {

UIStoryboard *storyboard;

// detect the height of our screen
int height = [UIScreen mainScreen].bounds.size.height;

if (height == 480) {
    storyboard = [UIStoryboard storyboardWithName:@"StoryboardIphone4s" bundle:nil];
    // NSLog(@"Device has a 3.5inch Display.");
} else {
    storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    // NSLog(@"Device has a 4inch Display.");
}

return storyboard;
}

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *storyboard = [self grabStoryboard];

    // show the storyboard
    self.window.rootViewController = [storyboard instantiateInitialViewController];
    [self.window makeKeyAndVisible];
return YES;
}

I have no errors at all: my project loads perfectly. But whatever the size, it shows the same storyboard. The NSLog's work perfectly, but the same Storyboard is loaded.

What could cause this?

Thanks

Upvotes: 0

Views: 582

Answers (2)

ateich
ateich

Reputation: 520

I believe your issue is with [storyboard instantiateInitialViewController]. It may not be loading the view, and thus Xcode is opting to not change the rootViewController instead of setting it to null.

Try setting the Storyboard ID of the initial iPhone4S view and loading it in grabStoryboard as shown below.

  1. In StoryboardIphone4s.storyboard, set your initial view's Storyboard ID to "main4SView".

  2. In AppDelegate.m change application didFinishLaunchingWithOptions to:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
        // show the storyboard
        [self grabStoryboard];
        [self.window makeKeyAndVisible];
        return YES;
    }
    
  3. In AppDelegate.m change grabStoryboard to:

    - (UIStoryboard *)grabStoryboard {
    
        UIStoryboard *storyboard;
    
        // detect the height of our screen
        int height = [UIScreen mainScreen].bounds.size.height;
    
        if (height == 480) {
            storyboard = [UIStoryboard storyboardWithName:@"StoryboardIphone4s" bundle:nil];
    
            //load the intial view controller from iPhone4S storyboard
            //and make it the rootViewController
            self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"main4SView"];
             NSLog(@"Device has a 3.5inch Display.");
        } else {
            storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
             NSLog(@"Device has a 4inch Display.");
        }
    
        return storyboard;
    }
    

Let me know how it goes.

Upvotes: 1

dogwasstar
dogwasstar

Reputation: 881

Could you print out the height variable and see what you get back. Might not be what you are expecting. Sorry couldnt add a comment apparently need 50 reputation

Upvotes: 0

Related Questions