简体   繁体   English

Xcode 6使用多个情节提要

[英]Xcode 6 using multiple storyboards

So I am making an iPhone app and I would like to have a different storyboard for each screen size. 因此,我正在制作一个iPhone应用程序,我希望每种屏幕尺寸都有一个不同的故事板。 (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. (iPhone 6为1,6 plus为1,4为1,5为1,1)我知道我可以使用大小类,但是我有理由要使用多个故事板。 Anyway In order to do this I put the following code in my app delegate... 无论如何,为了做到这一点,我在我的应用程序委托中放置了以下代码...

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 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. 它只能在我的iPhone 5上运行,而我的iPhone 5在iOS 7上运行。我确实删除了默认界面plist中的栏,但仍然无法使用。 So what can I do to make this work! 那么我该怎么做才能使这项工作成功! I would really appreciate your help! 我将衷心感谢您的帮助!

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. 正如Matt所说,将NSLogs放入代码中很重要,这样您才能知道正在运行的内容和未运行的内容。 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. 另一件事是,如果您没有启动图像文件,这将不适用于iPhone 6或6 plus。 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. 不仅是普通的xcasset,还必须有一个xib作为启动映像,该映像已添加到Xcode 6中。下面的代码,请注意,如果您在LANDSCAPE应用程序更改中使用它,则在#define中将其表示为高度。高到宽。

... This goes right under where it says "#import AppDelegate.h" ...就在它说“ #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]]; 如果您希望这样做,可以删除此行... 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. 无需从您的plist或常规页面中删除任何内容。

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. 另外,如果要添加六行,只需复制并粘贴另一条if语句并将其更改为#define IS_IPHONE_6_PLUS。 Just make sure you change it from vc1 2 or just vc to vc3 in both spots. 只要确保将两个位置的vc1 2或vc都更改为vc3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM