【问题标题】:Have separate storyboards for different screen sizes?有针对不同屏幕尺寸的单独故事板吗?
【发布时间】:2013-09-20 01:40:31
【问题描述】:

基本上我已经完成了一个应用程序。我唯一的问题 ATM 是该应用程序的设计仅考虑了 4 英寸显示屏。在 3.5 in 模拟器上运行时,应用程序会丢失 0.5 英寸(显然)。

那么,我的问题是,如何在 Xcode 5 中为不同的屏幕尺寸设置不同的故事板?

我知道在我可以使用以下代码之前:

-(void)initializeStoryBoardBasedOnScreenSize {

if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{    // The iOS device = iPhone or iPod Touch


CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;

if (iOSDeviceScreenSize.height == 480)
{   // iPhone 3GS, 4, and 4S and iPod Touch 3rd and 4th generation: 3.5 inch screen (diagonally measured)

    // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone35
    UIStoryboard *Main_iPhone2 = [UIStoryboard storyboardWithName:@"Main_iPhone3" bundle:nil];

    // Instantiate the initial view controller object from the storyboard
    UIViewController *initialViewController = [Main_iPhone2 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 (diagonally measured)

    // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
    UIStoryboard *Main_iPhone = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];

    // Instantiate the initial view controller object from the storyboard
    UIViewController *initialViewController = [Main_iPhone 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];
}

} else if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)

{   // The iOS device = iPad

UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;

}
}

但与 Xcode 5 的交易,以及我认为此代码不起作用的原因是,在您的项目中,有一个部分在一般情况下将故事板建立为整个特定设备类型的主要部分。

所以,要么有一种不同的方式来完成整个单独的故事情节,要么我的代码有问题。下面的代码显然是放在 AppDelegate.m 文件下的……所以不要认为我有什么问题。

谢谢,非常感谢您提供真正的帮助。我真的很想提交这个应用程序!!!

问候,帕特里西奥

【问题讨论】:

    标签: iphone ios objective-c xcode


    【解决方案1】:

    在我的 App 委托的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中,我有这个块:

    // Override point for customization after application launch.
    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:@"MainStoryboard35" bundle:nil];
            UIViewController *initViewController = [storyBoard instantiateInitialViewController];
            [self.window setRootViewController:initViewController];
        }
    }
    

    这会覆盖我的默认 MainStoryboard,用于 4 英寸屏幕,并加载我的 3.5 英寸屏幕故事板。在我的项目中,我仍然将 MainStoryboard(4 英寸屏幕版本)定义为主界面。

    这在 Xcode 5 中有效。而且它在一个运输应用程序中,所以它应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多