我或多或少完成了我的整个应用程序,显然只是意识到布局不适合 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 中,如下所示。
在 StoryboardIphone4s.storyboard 中,将初始 View 的 Storyboard ID 设置为“main4SView”。
在 AppDelegate.m 中将应用程序 didFinishLaunchingWithOptions 更改为:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ // show the storyboard [self grabStoryboard]; [self.window makeKeyAndVisible]; return YES; }
在 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/