2012-10-02 13 views
27

Al igual que una aplicación utiliza diferentes guiones gráficos para iPad y iPhone, me gustaría que mi aplicación utilizara un guión gráfico diferente para el iPhone 5. Como no hay opción en Info.plist para seleccionar el guión gráfico predeterminado para iPhone 5, ¿cómo lo haría? llamar programáticamente el guión gráfico?¿Cómo cambiar a Storyboard diferente para iPhone 5?

No quiero utilizar AutoLayout para esta aplicación a menos que sea absolutamente el último recurso. Entiendo cómo detectar si un usuario está usando un iPhone 5 u otro dispositivo con el mismo tamaño de pantalla. Solo necesito saber cómo configurar el guión gráfico predeterminado sin el plist.

Respuesta

55

que estaba buscando el mismo par de semanas la respuesta aquí es mi esperanza solución ayuda ..

-(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 *iPhone35Storyboard = [UIStoryboard storyboardWithName:@"Storyboard_iPhone35" 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 (diagonally measured) 

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

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

    } 
} 

Llame a este método bajo AppDelegate ddiFinishLaunchingWithOptions: Método Y también no se olvide el nombre de sus guiones gráficos correctamente

esperanza ayuda ...

+0

¡Gracias! Pasé demasiado tiempo buscando esta respuesta exacta. Problema sovled! – user1486548

+1

Me alegro de que funcionó ... – lionserdar

+0

El método compila bien, pero parece que no hace nada por mi parte. ¿Algunas ideas? – Klinetel

4

Esto funcionó para mí - ligero refinamiento con envoltura conseguir el guión gráfico de una función

-(UIStoryboard*) getStoryboard { 
    UIStoryboard *storyBoard = nil; 
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {   
     storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil]; 
    }else{ 
     if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone){ 
      // The iOS device = iPhone or iPod Touch 
      CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size; 
      if (iOSDeviceScreenSize.height == 480){ 
       // iPhone 3/4x 
       storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone_4" bundle:nil]; 

      }else if (iOSDeviceScreenSize.height == 568){ 
       // iPhone 5 etc 
       storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone_5" bundle:nil]; 
      } 
     } 
    } 

    ASSERT(storyBoard); 
    return storyBoard; 
} 

UIStoryboard* mainStoryBoard = [self getStoryboard]; 
    self.initialViewController = [mainStoryBoard instantiateInitialViewController]; 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.window.rootViewController = self.initialViewController; 
    [self.window makeKeyAndVisible]; 
+0

¿por qué el ASSERT (guión gráfico)? –

+0

solo un control de cordura de que existe el guión gráfico. ASSERT macro debería ser una opción en la versión de lanzamiento – gheese

+0

aha ... Nunca lo he usado realmente, así que no estaba seguro. ahora entiendo el contexto ... ¿algún tutorial recomendado sobre su uso? –

Cuestiones relacionadas