iOS - 为 iPhone 4S 加载不同的 Storyboard不起作用

标签 ios xcode uistoryboard

我或多或少完成了我的整个应用程序,显然只是意识到布局不适合 iPhone 4S。我可以开始使用自动布局,但它已经把我的项目搞砸了很多,所以我想到了另一种方法:为 iPhone 4s 加载另一个 Storyboard。 我在互联网上找到了以下代码,似乎对每个人都适用:

- (UIStoryboard *)grabStoryboard {

UIStoryboard *storyboard;

// detect the height of our screen
int height = [UIScreen mainScreen].bounds.size.height;

if (height == 480) {
    storyboard = [UIStoryboard storyboardWithName:@"StoryboardIphone4s" bundle:nil];
    // NSLog(@"Device has a 3.5inch Display.");
} else {
    storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    // NSLog(@"Device has a 4inch Display.");
}

return storyboard;
}

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *storyboard = [self grabStoryboard];

    // show the storyboard
    self.window.rootViewController = [storyboard instantiateInitialViewController];
    [self.window makeKeyAndVisible];
return YES;
}

我完全没有错误:我的项目加载完美。但无论大小,它都显示相同的 Storyboard。 NSLog 的工作完美,但加载了相同的 Storyboard。

什么可能导致这种情况?

谢谢

最佳答案

我认为您的问题出在 [storyboard instantiateInitialViewController] 上。它可能没有加载 View ,因此 Xcode 选择不更改 rootViewController 而不是将其设置为 null。

尝试设置初始 iPhone4S View 的 Storyboard ID 并将其加载到 grabStoryboard 中,如下所示。

  1. 在 StoryboardIphone4s.storyboard 中,将初始 View 的 Storyboard ID 设置为“main4SView”。

  2. 在 AppDelegate.m 中将应用程序 didFinishLaunchingWithOptions 更改为:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
        // show the storyboard
        [self grabStoryboard];
        [self.window makeKeyAndVisible];
        return YES;
    }
    
  3. 在 AppDelegate.m 中将 grabStoryboard 更改为:

    - (UIStoryboard *)grabStoryboard {
    
        UIStoryboard *storyboard;
    
        // detect the height of our screen
        int height = [UIScreen mainScreen].bounds.size.height;
    
        if (height == 480) {
            storyboard = [UIStoryboard storyboardWithName:@"StoryboardIphone4s" bundle:nil];
    
            //load the intial view controller from iPhone4S storyboard
            //and make it the rootViewController
            self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"main4SView"];
             NSLog(@"Device has a 3.5inch Display.");
        } else {
            storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
             NSLog(@"Device has a 4inch Display.");
        }
    
        return storyboard;
    }
    

告诉我进展如何。

关于iOS - 为 iPhone 4S 加载不同的 Storyboard不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27389499/

相关文章:

iOS 点播资源 : how to debug

ios - 我们如何将 Circle progress 绘制为 "three fourths"- 3/4?

ios - 设备 token NULL

ios - 我如何将数组元素搜索到文本字段中,当每个单词仅从数组中按下 textField 时,将建议其文本字段?

ios - 如何使 MGLPolyline 可点击?

ios - self.navigationController 始终为零

ios - 创建未知类型的图像格式是一个错误没有上传图像。 swift

ios - Xcode - 在我的应用程序上启用 iTunes 文件共享,但它没有出现在 iTunes 的列表中?

Xcode:命令 LinkStoryboards 失败,退出代码非零

ios - 应用程序在执行注销例程时崩溃