xcode 4.5如何在发布时选择故事板

时间:2012-09-21 19:05:05

标签: xcode storyboard uistoryboard xcode4.5

尝试让我的应用程序同时适用于iPhone 5和iPhone 4 / 4s。我尝试了“AutoLayout”,但似乎不适用于我的应用程序还读取它在iOS 5中不受支持.AutoLayout特别在具有UIScrollview和UIPicker的视图控制器上失败,该UIPicker在代码中重新调整大小。有两个故事板一个4英寸和一个3.5英寸似乎是要走的路。

两个Storyboard aproch似乎是我的解决方案。所以这给我留下了两个问题;

  1. 如果是4 / 4s / 5那么代码应该检测到哪里去?我会假设在didFinishLaunchingWithOptions方法的appDelegate.m中

  2. 如何更改“主要故事板”?

5 个答案:

答案 0 :(得分:61)

这是一个很好的问题。

您需要做的是,

  1. 选择您当前的4 / 4s故事板,转到文件,复制,然后给它一个iPhone 5特定名称。 确保已选中目标和您的应用名称。

  2. 接下来,您必须在故事板中选择场景,并在“属性”检查器中将大小更改为“Retina 4全屏”。 这允许您重新排列此显示的所有内容。

  3. 最后在应用程序中,didFinishLaunchingWithOptions将以下代码粘贴到您为4英寸故事板提供的故事板名称。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
        UIStoryboard *storyBoard;
    
        CGSize result = [[UIScreen mainScreen] bounds].size;
        CGFloat scale = [UIScreen mainScreen].scale;
        result = CGSizeMake(result.width * scale, result.height * scale);
    
        if(result.height == 1136){
            storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone_5" bundle:nil];
            UIViewController *initViewController = [storyBoard instantiateInitialViewController];
            [self.window setRootViewController:initViewController];
        }
    }
    
    return YES;
    }
    

  4. 如果有人不知道如何执行第1步,请执行以下操作。

    1. 转到项目目录并复制粘贴MainStoryboard.storyboard并将新故事板重命名为MainStoryboard5.storyboard

    2. 右键单击Project并点击MainStoryboard5.storyboard

    3. ,在项目中(在Xcode中)添加此新故事板Add Files to ....
    4. 现在我们在xcode中有两个故事板。

    5. 提示

      您可能必须使用'产品>完成上述所有操作后,请将其清理干净。

答案 1 :(得分:7)

目前唯一的方法是检查您使用的iPhone 5是[UIScreen mainScreen] bounds]还是[[UIScreen mainScreen] scale]

BOOL isIphone5 = (([[UIDevice currentDevice] userInterfaceIdiom] 
== UIUserInterfaceIdiomPhone) && (([UIScreen mainScreen].bounds.size.height * 
[[UIScreen mainScreen] scale]) >= 1136));

只有在您的应用程序中至少添加了Default-568h@2x.png启动图像时,此功能才有效。否则这将总是返回false。 (因为如果您没有启动图像,屏幕将被设置为letterbox)

要将故事板设置为iPhone 5版本,您可能需要查看this question

答案 2 :(得分:2)

我从fields.cage回答中获取灵感并将其改编为我的代码,它运行正常。谢谢!!

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
        UIStoryboard *storyBoard;

        CGSize result = [[UIScreen mainScreen] bounds].size;
        CGFloat scale = [UIScreen mainScreen].scale;
        result = CGSizeMake(result.width * scale, result.height * scale);

        if(result.height == 1136){


             self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone5" bundle:nil] autorelease];

        }
        else
        {
             self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
        }
    }

答案 3 :(得分:1)

我最近在做的是在我需要检查设备的任何类中添加一个define语句。这也可以在任何全局头文件中完成。

#define IS_IPHONE (!IS_IPAD)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)

布尔测试来自Detect iphone 5 4" screen

bool isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136));
if (isiPhone5) {
        // Setup For iPhone 5 Screen Size
    UIStoryboard *storyBoard;

    storyBoard = [UIStoryboard storyboardWithName:@"MyiPhone5StoryboardName" bundle:nil];
    UIViewController *initViewController = [storyBoard instantiateInitialViewController];
    [self.window setRootViewController:initViewController];
}

如果您已经在使用故事板,这种方法很有效,并且只想从您的项目为iPhone 5设备开始的默认值更改故事板。如果您从头开始使用现有的非故事板项目,您可以这样做。

#define IS_IPHONE (!IS_IPAD)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)

bool isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136));
if (isiPhone5) {
        // Load iPhone 5 Storyboard
    UIStoryboard *storyBoard;

    storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone5" bundle:nil];
    UIViewController *initViewController = [storyBoard instantiateInitialViewController];
    [self.window setRootViewController:initViewController]; 
}

else if (IS_IPAD) {
        // Load IPAD StoryBoard
    UIStoryboard *storyBoard;

    storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
    UIViewController *initViewController = [storyBoard instantiateInitialViewController];
    [self.window setRootViewController:initViewController]; 
}

else {
        // Load the iPhone 3.5" storyboard
    UIStoryboard *storyBoard;

    storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    UIViewController *initViewController = [storyBoard instantiateInitialViewController];
    [self.window setRootViewController:initViewController]; 
}

当我开始一个项目时,我在故事板中设计了iPhone 3.5“版本(如果我使用的是故事板),那么当我完成该设计时,我会进入我的项目文件并找到故事板文件。自故事板文件以来只是一个XML布局文件,我可以将该文件加载到我最喜欢的文本编辑器中并更改两个标签。

将iPhone转换为iPad

  1. 在文件顶部找到 targetRuntime="iOS.CocoaTouch
  2. 更改为 targetRuntime="iOS.CocoaTouch.iPad"
  3. 您可以在文件底部找到 <simulatedScreenMetrics key="destination" type="retina4"/>
  4. 将其更改为<simulatedScreenMetrics key="destination"/>
  5. 只有在为4英寸iPhone屏幕设置主故事板文件时,才会显示最后一项。

    重要的是,如果您只将iPhone 5添加到现有项目中,您只需要首先检查以覆盖默认值,并加载您的特殊故事板文件。这实际上使我不必手动布局iPhone 5代码中的所有对象。

答案 4 :(得分:0)

第一个工作非常好简单明了,但对于所有人来说,如果你想集成多个设备,请看这里:

  1. 使用名为Storyboard的iPhone5创建项目 iPhone5_Storyboard。

  2. 关闭应用并在文件夹内克隆主要内容 名为iPhone5_Storyboard的故事并重命名为iPhone4_Storyboard,不要忘记更改大小并重新排序xcode界面构建器中的所有对象。

  3. 如果你想要一个通用版本,可选择在xcode中使用Add File 并为iPad添加新的Storyboard EMPTY目标,打开联系 iphone5_storyboard使用CMD + A并复制全部并粘贴在Empty中 适用于iPad的故事(需要重新排序对象,但所有链接和工作都已完成)。

  4. 在TARGET下的Xcode中设置Universal App并在MainStoryboard下设置iPhone5_Storyboard,在iPad Main Story设置iPad_Storyboard下。

  5. 现在感谢此帖的第一个答案我们必须在 AppDelegate.m 中使用此代码实现所有不同的故事: < / LI>

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

        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
            UIStoryboard *storyBoard;
    
            CGSize result = [[UIScreen mainScreen] bounds].size;
            CGFloat scale = [UIScreen mainScreen].scale;
            result = CGSizeMake(result.width *scale, result.height *scale);
    
    //----------------HERE WE SETUP FOR IPHONE4/4s/iPod----------------------
    
            if(result.height == 960){
                storyBoard = [UIStoryboard storyboardWithName:@"iPhone4_Storyboard" bundle:nil];
                UIViewController *initViewController = [storyBoard instantiateInitialViewController];
                [self.window setRootViewController:initViewController];
            }
    
    //----------------HERE WE SETUP FOR IPHONE3/3s/iPod----------------------
    
            if(result.height == 480){
                storyBoard = [UIStoryboard storyboardWithName:@"iPhone4_Storyboard" bundle:nil];
                UIViewController *initViewController = [storyBoard instantiateInitialViewController];
                [self.window setRootViewController:initViewController];
            }
        }
    
            return YES;
     }
    

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){ UIStoryboard *storyBoard; CGSize result = [[UIScreen mainScreen] bounds].size; CGFloat scale = [UIScreen mainScreen].scale; result = CGSizeMake(result.width *scale, result.height *scale); //----------------HERE WE SETUP FOR IPHONE4/4s/iPod---------------------- if(result.height == 960){ storyBoard = [UIStoryboard storyboardWithName:@"iPhone4_Storyboard" bundle:nil]; UIViewController *initViewController = [storyBoard instantiateInitialViewController]; [self.window setRootViewController:initViewController]; } //----------------HERE WE SETUP FOR IPHONE3/3s/iPod---------------------- if(result.height == 480){ storyBoard = [UIStoryboard storyboardWithName:@"iPhone4_Storyboard" bundle:nil]; UIViewController *initViewController = [storyBoard instantiateInitialViewController]; [self.window setRootViewController:initViewController]; } } return YES; }

    现在我们有一个适用于iPhone 3 / 3Gs / 4 / 4s / 5 iPod(AllGen)iPad 1/2/3/4 Mini和Retina的通用应用程序

    谢谢你们