启动特定的故事板

时间:2012-10-22 15:11:24

标签: storyboard

如果设备是iPhone 5,我需要启动特定的故事板,如果设备是iPhone 4S或更早版本,我需要启动另一个故事板。我知道我需要添加一些代码来做didFinishLaunchingWithOptions方法,但我不知道究竟是哪个!

任何人都可以给我正确的代码吗?

3 个答案:

答案 0 :(得分:1)

在这个问题上有一个很好的解决方案:

xcode 4.5 how to pick storyboards at launch

基本上,您可以在didFinishLaunchingWithOptions中检查屏幕高度并加载相应的记分板。

答案 1 :(得分:0)

您必须定义两个不同的故事板。一个用于iPhone 4尺寸,一个用于iPhone 5尺寸。

然后将以下代码添加到您的应用程序委托应用程序didFinishLaunchingWithOptions方法...

// Override point for customization after application launch.
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;

if (iOSDeviceScreenSize.height == 480)
{
    // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone35
    UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:@"iPhone4" 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
    // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
    UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"iPhone5" bundle:nil];

    UIViewController *initialViewController = [iPhone4Storyboard instantiateInitialViewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController  = initialViewController;
    [self.window makeKeyAndVisible];
}
return YES;

或者你可以随时尝试使用autolayout。无论哪种方式都有效。

答案 2 :(得分:0)

您可以检查iPhone 4 / iPhone 5并根据它实例化故事板

以下代码甚至会告诉您iPhone或iPad。

可以在整个App中全局保存iPhone或iPad bool。

注意:默认情况下,iphone4 storyboard将被实例化。

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


iPhone5 = NO;
iPad = NO;

// Override point for customization after application launch.
UIStoryboard *storyBoard;

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    if ([UIScreen mainScreen].scale == 2.0f)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        CGFloat scale = [UIScreen mainScreen].scale;
        result = CGSizeMake(result.width * scale, result.height * scale);

        if(result.height == 960)
        {
            iPhone5 = NO;
           // NSLog(@"iPhone 4, 4s Retina Resolution");
        }
        if(result.height == 1136)
        {
            iPhone5 = YES;
          //  NSLog(@"iPhone 5 Resolution");
            storyBoard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
            UIViewController *tabBarController = [storyBoard instantiateInitialViewController];
            self.window.rootViewController = tabBarController ;
        }
    }
    else
    {
      //  NSLog(@"iPhone Standard Resolution");
        iPad = YES;
    }
}
else
{
    iPad = YES;
}

return YES;

}