根据设备类型更改UIStoryboards

时间:2014-08-20 14:42:25

标签: ios objective-c iphone xcode ipad

我开始使用仅支持iPhone 4尺寸屏幕的iPhone应用程序,我想将其转换为也适用于iPad的通用应用程序,因此我创建了一个名为“Storyboard-iPad”的新故事板并输入以下代码进入我的AppDelegate.m。这不起作用,因为当我启动应用程序时,它仍适用于iPhone 5& 4个大小的屏幕,但是当我在iPad模拟器上运行它时,它就像左上角的iPhone 5版本一样。有谁知道我做错了什么以及我需要做些什么来解决这个错误?

非常感谢任何帮助,

谢谢!

AppDelegate.m

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 == 960){
        storyBoard = [UIStoryboard storyboardWithName:@"Storyboard-iPhone4" bundle:nil];
        UIViewController *initViewController = [storyBoard instantiateInitialViewController];
        [self.window setRootViewController:initViewController];
    }

    else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){

        storyBoard = [UIStoryboard storyboardWithName:@"Storyboard-iPad"

                                                   bundle: nil];
    }
}

1 个答案:

答案 0 :(得分:0)

您尝试编写代码来执行iOS自动执行的操作。

重命名您的iPhone故事板Storyboard~iphone。重命名您的iPad故事板Storyboard~ipad。在应用程序目标设置中将主界面名称设置为Storyboard

如果您使用这些名称,使用波浪号和小写设备字符串,资源加载器将自动为当前设备选择适当的资源。

the Bundle Programming Guide: “Device-Specific Resources in iOS”中记录了这一点。

相关问题