【发布时间】:2019-05-05 23:38:34
【问题描述】:
如果设备是 iPhone 5,我需要启动一个特定的故事板,如果设备是 iPhone 4S 或更早版本,我需要启动另一个故事板。我知道我需要添加一些代码来执行 didFinishLaunchingWithOptions 方法,但我不知道具体是哪个!
谁能给我正确的代码?
【问题讨论】:
标签: storyboard
如果设备是 iPhone 5,我需要启动一个特定的故事板,如果设备是 iPhone 4S 或更早版本,我需要启动另一个故事板。我知道我需要添加一些代码来执行 didFinishLaunchingWithOptions 方法,但我不知道具体是哪个!
谁能给我正确的代码?
【问题讨论】:
标签: storyboard
这个问题有一个很好的解决方案:
xcode 4.5 how to pick storyboards at launch
基本上,您可以在 didFinishLaunchingWithOptions 中检查屏幕高度并加载相应的记分牌。
【讨论】:
您必须定义两个不同的故事板。一种用于 iPhone 4 尺寸,一种用于 iPhone 5 尺寸。
然后将以下代码添加到您的应用程序委托应用程序的 didFinishLaunchingWithOptions 方法中...
// Override point for customization after application launch.
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if (iOSDeviceScreenSize.height == 480)
{
// Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone35
UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:@"iPhone4" bundle:nil];
// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iPhone35Storyboard 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
// Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"iPhone5" bundle:nil];
UIViewController *initialViewController = [iPhone4Storyboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = initialViewController;
[self.window makeKeyAndVisible];
}
return YES;
或者您可以随时尝试使用自动布局来碰碰运气。无论哪种方式都有效。
【讨论】:
您可以检查 iPhone 4/iPhone 5 并根据它实例化故事板
以下代码甚至可以告诉您 iPhone 或 iPad。
可以在整个应用程序中全局保存 iPhone 或 iPad bool。
注意:默认情况下 iphone4 故事板将被实例化。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
iPhone5 = NO;
iPad = NO;
// Override point for customization after application launch.
UIStoryboard *storyBoard;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if ([UIScreen mainScreen].scale == 2.0f)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
if(result.height == 960)
{
iPhone5 = NO;
// NSLog(@"iPhone 4, 4s Retina Resolution");
}
if(result.height == 1136)
{
iPhone5 = YES;
// NSLog(@"iPhone 5 Resolution");
storyBoard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
UIViewController *tabBarController = [storyBoard instantiateInitialViewController];
self.window.rootViewController = tabBarController ;
}
}
else
{
// NSLog(@"iPhone Standard Resolution");
iPad = YES;
}
}
else
{
iPad = YES;
}
return YES;
}
【讨论】: