BookController в iOS 5 «Поваренная книга разработчика» (Эрика Садун) некорректно запускается в ландшафтном режиме - PullRequest
1 голос
/ 04 декабря 2011

Проблема, которую я пытаюсь решить:

Я использовал главу 05, пример 06, BookController в главе 5 (C05 / 06-Scrubbing Pages in https://github.com/erica/iOS-5-Cookbook.git) в проекте, в котором есть UInavigatioControllerкак rootViewController, но он не запускается правильно в ландшафтном режиме.Если вы поворачиваете устройство в вертикальной ориентации и возвращаетесь в альбомную ориентацию, оно начинает работать.

Чтобы показать ошибку, измените пример делегата тестового стенда в файле main.c следующим образом:

#pragma mark Application Setup
@interface TestBedAppDelegate : NSObject <UIApplicationDelegate>
{
    UIWindow *window;
}
@end
@implementation TestBedAppDelegate

TestBedViewController *tbvc;
UINavigationController *navigationController;

- (void) pushBookController: (UIGestureRecognizer *) recognizer
{
    [navigationController pushViewController:tbvc animated:YES];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{   
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    tbvc = [[TestBedViewController alloc] init];

    UIViewController *start = [tbvc controllerWithColor:[UIColor whiteColor] withName:@"white"];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushBookController:)];
    [start.view addGestureRecognizer:tap];

    navigationController = [[UINavigationController alloc] initWithRootViewController:start];

    window.rootViewController = navigationController;
    [window makeKeyAndVisible];
    return YES;
}
@end

Мое приложениезапускает альбомную ориентацию, поэтому проблема в том, что книжный контроллер не распознает альбомный режим при запуске (если вы поворачиваете после запуска, он будет работать).Попробуйте.

Чтобы распознать ландшафт при запуске, замените в BookController следующий метод

// Entry point for external move request
- (void) moveToPage: (uint) requestedPage
{
    //[self fetchControllersForPage:requestedPage orientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation];
    [self fetchControllersForPage:requestedPage orientation:self.interfaceOrientation];
}

и добавьте следующее:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {return YES;}

Теперь проблема в том, что BookController не появляется при запуске, но опять же, если вы поворачиваете, он начинает работать.

1 Ответ

1 голос
/ 09 февраля 2012

Возможно, вы уже решили это, но попробуйте изменить экземпляр Книги так:

+ (id) bookWithDelegate: (id) theDelegate
{   

    NSDictionary *options = nil;
    //Check the orientation
    if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        //Setting Spine Location to middle for Landscape orientation
        options =  [NSDictionary dictionaryWithObject:
                    [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid]
                                           forKey: UIPageViewControllerOptionSpineLocationKey];
    }

    BookController *bc = [[BookController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];

    bc.dataSource = bc;
    bc.delegate = bc;
    bc.bookDelegate = theDelegate;

    return bc;
}

Обратите внимание, что я добавляю проверку statusBarOrientation, чтобы установить позвоночник.

По сути, вам нужно проверить ориентацию при создании UIPageViewController, и я использовал statusBarOrientation.

О, и не забудьте убедиться, что вы получаете правильную ориентацию

- (BOOL) useSideBySide: (UIInterfaceOrientation) orientation
{
    BOOL isLandscape = UIInterfaceOrientationIsLandscape(orientation);
    // in case the UIInterfaceOrientation is 0.
    if (!orientation) {
        isLandscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
    }
    return isLandscape;    
}

ps: метод bookWithDelegate соответствует классу BookController в примере, упомянутом в вопросе.

...