1

我试图解决的问题:

我在第 5 章中使用了第 05 章示例 06 BookController(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不会在开始时出现,但如果你旋转它又开始工作。

4

1 回答 1

1

可能您已经解决了这个问题,但是尝试像这样更改 Book 的实例化:

+ (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 类。

于 2012-02-09T14:46:02.413 回答