2012-10-02 35 views
27

Giống như một ứng dụng sử dụng các bảng phân cảnh khác nhau cho iPad và iPhone, tôi muốn ứng dụng của tôi sử dụng bảng phân cảnh khác nhau cho iPhone 5. Vì không có tùy chọn trong Info.plist để chọn bảng phân cảnh mặc định cho iPhone 5, làm cách nào tôi lập trình gọi bảng phân cảnh?Làm cách nào để chuyển sang Storyboard khác cho iPhone 5?

Tôi không muốn sử dụng AutoLayout cho ứng dụng này trừ khi đó hoàn toàn là phương sách cuối cùng. Tôi hiểu cách phát hiện xem người dùng có đang sử dụng iPhone 5 hoặc thiết bị khác có cùng kích thước màn hình hay không. Tôi chỉ cần biết cách đặt bảng phân cảnh mặc định mà không có plist.

Trả lời

55

tôi đang tìm kiếm các cặp vợ chồng trả lời tương tự của tuần trước, đây là giải pháp hy vọng tôi giúp ..

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

    } 
} 

Gọi phương pháp này dưới appdelegate ddiFinishLaunchingWithOptions: Phương pháp Và cũng đừng quên tên cốt truyện của bạn đúng

Hope giúp ...

+0

Cảm ơn bạn! Tôi đã dành quá nhiều thời gian để tìm câu trả lời chính xác này. Đã giải quyết vấn đề! – user1486548

+1

Tôi rất vui vì nó đã hoạt động ... – lionserdar

+0

Phương pháp này biên dịch tốt, nhưng nó dường như không thực sự làm bất cứ điều gì ở phần cuối của tôi. Bất kỳ ý tưởng? – Klinetel

4

này làm việc cho tôi - tinh tế nhẹ với gói nhận được kịch bản trong một chức năng

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

lý do tại sao ASSERT (bảng phân cảnh)? –

+0

chỉ là một kiểm tra sanity rằng bảng phân cảnh tồn tại. Macro ASSERT phải là không có op trong bản phát hành – gheese

+0

aha ... Tôi chưa bao giờ thực sự sử dụng nó, vì vậy không chắc chắn. tôi nhận được bối cảnh ngay bây giờ ... bất kỳ hướng dẫn được đề nghị về việc sử dụng chúng? –

Các vấn đề liên quan