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

xcode 4.5如何在发布时选择故事板,xcode,storyboard,uistoryboard,xcode4.5,Xcode,Storyboard,Uistoryboard,Xcode4.5,试图使我的应用程序同时适用于iPhone 5和iPhone 4/4s。我尝试了“自动布局”,但似乎对我的应用程序不起作用。我还读到iOS 5不支持它。AutoLayout在具有UIScrollview和在代码中重新调整大小的UIPicker的视图控制器上特别失败。拥有两个故事板,一个是4英寸,一个是3.5英寸,这似乎是一个不错的选择 两个故事板似乎是我的解决方案。这给我留下了两个问题 如果是4/4s/5 go,代码应该在哪里检测?我会在appDelegate.m中假设didfishlaunchi

试图使我的应用程序同时适用于iPhone 5和iPhone 4/4s。我尝试了“自动布局”,但似乎对我的应用程序不起作用。我还读到iOS 5不支持它。AutoLayout在具有UIScrollview和在代码中重新调整大小的UIPicker的视图控制器上特别失败。拥有两个故事板,一个是4英寸,一个是3.5英寸,这似乎是一个不错的选择

两个故事板似乎是我的解决方案。这给我留下了两个问题

  • 如果是4/4s/5 go,代码应该在哪里检测?我会在appDelegate.m中假设didfishlaunchingwithoptions方法

  • 如何更改“主情节提要”


  • 目前唯一的方法是检查您是否正在使用iPhone 5是
    [UIScreen mainScreen]界限]
    [[UIScreen mainScreen]比例]

    BOOL isIphone5 = (([[UIDevice currentDevice] userInterfaceIdiom] 
    == UIUserInterfaceIdiomPhone) && (([UIScreen mainScreen].bounds.size.height * 
    [[UIScreen mainScreen] scale]) >= 1136));
    
    仅当您至少添加了
    默认值时,此选项才有效-568h@2x.png
    将映像启动到应用程序。否则它将始终返回false。(因为如果您没有启动图像,屏幕将显示字母框)

    要将情节提要设置为iPhone 5版本,您可能需要看一看这是一个很好的问题。

    你需要做的是

  • 选择您当前的4/4s情节提要,转到文件,复制,然后为其指定一个特定于iPhone 5的名称。 确保已选中目标和应用程序名称。

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

  • 最后,在application 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;
    }
    

  • 如果有人不知道如何执行步骤1,请按以下步骤操作。

  • 转到项目目录,复制粘贴
    MainStoryboard.storyboard
    ,并将新的故事板重命名为
    MainStoryboard 5.storyboard

  • 通过右键单击project并单击
    将文件添加到..

  • 现在我们在xcode中有两个故事板

  • 提示


    在完成以上所有操作后,您可能需要使用“产品>清洁”来实现此功能。

    我从fields.cage answer中获得了灵感,并将其改编为我的代码,效果很好。谢谢

    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];
            }
        }
    

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

    #define IS_IPHONE (!IS_IPAD)
    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
    
    布尔测试是从

    如果您已经在使用情节提要,并且只想更改项目开始时针对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

  • 在文件顶部找到
    targetRuntime=“iOS.CocoaTouch
  • 更改为
    targetRuntime=“iOS.CocoaTouch.iPad”
  • 在文件的底部,您可以找到这个
  • 将此更改为
  • 只有在为4英寸iPhone屏幕设置了主情节提要文件时,最后一项才会出现。


    这里重要的是,如果您只需将iPhone5添加到现有项目中,您只需进行第一次检查即可覆盖默认值,并加载特殊的情节提要文件。这让我不用在iPhone 5的代码中手动布局所有对象

    第一个非常好的方法简单明了,但是如果你想集成乘法设备,请看这里:

  • 为iPhone5创建一个名为 iPhone5_故事板

  • - (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;
    }
    
  • 关闭应用程序并在文件夹内克隆主应用程序 故事名为iPhone5_情节提要并重命名为iPhone4_情节提要不要忘记在xcode interface builder中更改大小并重新排序所有对象

  • 如果您想要一个通用版本,可以选择在xcode内部使用Add File 并为iPad添加了新的故事板空目标 iphone5_情节提要使用CMD+A,复制所有内容并粘贴到空白处 iPad的故事(需要对对象重新排序,但都是链接和工作的)

  • 在Xcode中的TARGET set Universal App和MainStoryboard下的设置中,您可以看到iPhone5_故事板,在iPad主故事设置iPad_故事板下

  • - (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;
    }
    
  • 现在多亏了这篇文章的第一个答案我们必须在AppDelegate.m中使用这段代码来实现所有不同的故事:

  • 
    -(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{

        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的通用应用程序


    谢谢你的家伙

    你也应该考虑使用Struts和Struts来调整你的视图,而不是复制你的故事板,特别是如果所有正在改变的是布局。它可以很容易地导致一个更可维护的解决方案,并支持iOS的先前版本。使用所提供的答案,我创建了一个类别,使之更容易。--非常简单…或者谢谢。在运行项目之前,请确保您已经添加了启动屏幕:)我最近开发了另一个应用程序,并在故事板中构建了所有4英寸显示屏。在t上运行时