如果设备是 iPhone 5,我需要启动一个特定的故事板,如果设备是 iPhone 4S 或更早版本,我需要启动另一个故事板。我知道我需要添加一些代码来执行 didFinishLaunchingWithOptions 方法,但我不知道具体是哪个!
任何人都可以给我正确的代码吗?
如果设备是 iPhone 5,我需要启动一个特定的故事板,如果设备是 iPhone 4S 或更早版本,我需要启动另一个故事板。我知道我需要添加一些代码来执行 didFinishLaunchingWithOptions 方法,但我不知道具体是哪个!
任何人都可以给我正确的代码吗?
您可以检查 iPhone 4/iPhone 5 并根据它实例化故事板
下面的代码甚至告诉你 iPhone 或 iPad。
可以通过 App 全局保存 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;
}
您必须定义两个不同的故事板。一种用于 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;
或者您可以随时使用自动布局来试试运气。无论哪种方式都有效。