2012-10-02 5 views
27

Genau wie eine App verschiedene Storyboards für iPad und iPhone verwendet, möchte ich, dass meine App ein anderes Storyboard für das iPhone 5 verwendet. Da es in der Info.plist keine Option zur Auswahl des Standard-Storyboards für iPhone 5 gibt, wie würde ich programmatisch das Storyboard aufrufen?Wie man zu anderem Storyboard für iPhone 5 wechselt?

Ich möchte nicht AutoLayout für diese App verwenden, es sei denn, es ist absolut der letzte Ausweg. Ich verstehe, wie man erkennt, wenn ein Benutzer ein iPhone 5 oder ein anderes Gerät mit der gleichen Bildschirmgröße verwendet. Ich muss nur wissen, wie man das Standard-Storyboard ohne den Plist setzt.

Antwort

55

Ich wurde für die gleiche Antwort paar Wochen suchen vor hier ist meine Lösung Hoffnung hilft ..

-(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; 

    } 
} 

Rufen Sie diese Methode unter AppDelegate ddiFinishLaunchingWithOptions: Methode Und auch nicht vergessen, den Namen Ihres Storyboards richtig

Hoffnung hilft ...

+0

Vielen Dank! Ich habe viel zu lange nach dieser genauen Antwort gesucht. Problem gelöst! – user1486548

+1

Ich bin froh, es hat funktioniert ... – lionserdar

+0

Die Methode kompiliert gut, aber es scheint nicht wirklich etwas zu meinem Ende zu tun. Irgendwelche Ideen? – Klinetel

4

das ist für mich gearbeitet - leichte Verfeinerung mit Verpackung bekommt das Storyboard in einer Funktion

-(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

warum das ASSERT (Storyboard)? –

+0

nur eine Vernunftprüfung, dass das Storyboard existiert. ASSERT Makro sollte ein No Op in Release-Build sein – gheese

+0

Aha ... Ich habe es nie wirklich benutzt, so war mir nicht sicher. Ich bekomme den Kontext jetzt ... Empfohlene Tutorials zu deren Verwendung? –