Selecting different storyboards based on device type

て烟熏妆下的殇ゞ 提交于 2019-11-28 13:22:56

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

This is another thing you can set directly within your info.plist file. No need for any programming efforts. Look for the property named 'Main storyboard file base name' that will have 'Main' in it by default.

You can add another property named 'Main storyboard file base name (iPad)' that will then get used for iPad.

This is what the raw output in the plist looks like:

<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIMainStoryboardFile~ipad</key>
<string>iPad</string>

Afaik it is also possible to simply add a second storyboard named Main~iPad.storyboard (if the UIMainStoryboardFile key is set as Main). And that will get picked up for iPads. Haven't tested this in a while though.

// In appdelegate class, while launching application select specified story board.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        UIStoryboard *storyboard1;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        storyboard1 = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]];
    }
    else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        storyboard1 = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:[NSBundle mainBundle]];
    }
    UIViewController *vc = [storyboard instantiateInitialViewController];

    // Set root view controller and make windows visible
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];

    return YES;
}

You can name your storyboard like this

  • Main.storyboard (for iPhone)
  • Main_iPad.storyboard (for iPad)

and select them like this

- (UIStoryboard *)deviceStoryboardWithName:(NSString *)name bundle:(NSBundle *)bundle {
    if (IS_IPAD) {
        NSString *storyboardIpadName = [NSString stringWithFormat:@"%@_iPad", name];
        NSString *path = [[NSBundle mainBundle] pathForResource:storyboardIpadName ofType:@"storyboardc"];

        if (path.length > 0) {
            return [UIStoryboard storyboardWithName:storyboardIpadName bundle:bundle];
        }
    }


    return [UIStoryboard storyboardWithName:name bundle:bundle];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!