Как переключиться на другую раскадровку для iPhone 5?

Точно так же, как приложение использует разные раскадровки для iPad и iPhone, я хотел бы, чтобы мое приложение использовало другую раскадровку для iPhone 5. Поскольку в Info.plist нет возможности выбрать раскадровку по умолчанию для iPhone 5, как бы я программно вызывал раскадровка?

Я не хочу использовать AutoLayout для этого приложения, если только это не крайняя мера. Я понимаю, как определить, использует ли пользователь iPhone 5 или другое устройство с таким же размером экрана. Мне просто нужно знать, как установить раскадровку по умолчанию без plist.


person user1486548    schedule 02.10.2012    source источник


Ответы (2)


Я искал тот же ответ пару недель назад, вот мое решение, надеюсь, поможет.

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

    }
}

Вызовите этот метод в AppDelegate ddiFinishLaunchingWithOptions: method А также не забудьте правильно назвать свои раскадровки

Надежда помогает...

person lionserdar    schedule 02.10.2012
comment
Спасибо! Я потратил слишком много времени на поиск точного ответа. Проблема решена! - person user1486548; 02.10.2012
comment
Метод компилируется нормально, но, похоже, он ничего не делает с моей стороны. Есть идеи? - person Klinetel; 11.01.2013
comment
Этот код работает отлично! Спасибо @lionserdar. Сэкономил мне много времени. - person pinyin_samu; 19.06.2013
comment
Большое спасибо, дорогая. Очень мне помог. Еще раз спасибо - person Abdul Yasin; 22.10.2013

Это сработало для меня - небольшая доработка с переносом раскадровки в функцию

-(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];
person gheese    schedule 24.01.2013
comment
почему ASSERT (раскадровка)? - person jesses.co.tt; 19.12.2013
comment
просто проверка здравомыслия, что раскадровка существует. Макрос ASSERT не должен быть в сборке релиза. - person gheese; 29.12.2013
comment
ага... Я никогда им не пользовался, так что не был уверен. теперь я понимаю контекст ... какие-либо рекомендуемые учебные пособия по их использованию? - person jesses.co.tt; 05.01.2014