在UINavigationController中嵌套UIPageViewController只显示一个页面

时间:2012-09-13 08:59:18

标签: uinavigationcontroller uipageviewcontroller

我花了好几个小时才开始工作。希望有人能帮助我。

我有一个UIPageViewController,当我在应用程序启动时将它添加到我的默认视图时,它可以正常工作。这就是我的工作:

//Step 1
//Instantiate the UIPageViewController.
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
                                                          navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

//Step 2:
//Assign the delegate and datasource as self.
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;

//Step 3:
//Set the initial view controllers.
ContentViewController *contentViewController = [[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
contentViewController.labelContents = [self.modelArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:contentViewController];
[self.pageViewController setViewControllers:viewControllers
                                  direction:UIPageViewControllerNavigationDirectionForward
                                   animated:NO
                                 completion:nil];


[self addChildViewController:self.pageViewController];
[self.view addSubview: self.pageViewController.view];

现在我想在初始视图中使用导航控制器,然后在用户单击按钮时将UIPageViewController推送到堆栈上。这也有效,但是当我处于横向模式时,UIPageViewController只显示一个页面(水平拉伸),而不是两个,因为它应该是。我需要将设备旋转为纵向,然后返回横向模式以强制UIPageViewController显示两个页面。 我将UIPageViewController添加到导航控制器:

[self.navController pushViewController:self.pageViewController animated:NO];

当我调试我的代码时,我看到没有导航控制器,UIPageViewController的委托方法在启动时被调用。当我在导航控制器上按下UIPageViewController时,直到我旋转设备才会调用它们。

有谁知道如何解决这个问题?提前感谢您提供任何帮助/提示。

1 个答案:

答案 0 :(得分:3)

this thread的帮助下计算出来。

关键是在通过选项字典创建时,在pageViewController上设置spineLocation:

    options = [NSDictionary dictionaryWithObject: [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid]
                                          forKey: UIPageViewControllerOptionSpineLocationKey];

此选项应放在支票内,以查看当前界面方向。

在上述线程中,接口方向由三元运算符检查;我把它放在一个if语句中,以便我可以使用相同的机会将额外的UIViewController添加到viewControllers数组上。

如果界面方向是纵向的,那么在创建pageViewController时'options'仍然是nil,因此它将继续以纵向模式创建它,只有一个viewController。

这是我在BookViewController中的整个viewDidLoad代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    _modelController = [[ModelController alloc] init];

    DataViewController *startingViewController = [self.modelController viewControllerAtIndex:0
                                                                                  storyboard:self.storyboard];

    NSMutableArray *viewControllers = [NSMutableArray arrayWithObjects:
                                startingViewController,
                                nil];
    NSDictionary *options;
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {

        DataViewController *secondViewController = [self.modelController viewControllerAtIndex:1
                                                                                    storyboard:self.storyboard];

        [viewControllers addObject:secondViewController];

        options = [NSDictionary dictionaryWithObject: [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid]
                                              forKey: UIPageViewControllerOptionSpineLocationKey];
    }

    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
                                                              navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                                                            options:options];
    self.pageViewController.delegate = self;

    [self.pageViewController setViewControllers:viewControllers
                                      direction:UIPageViewControllerNavigationDirectionForward
                                       animated:NO
                                     completion:NULL];

    self.pageViewController.dataSource = self.modelController;

    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];

    // Set the page view controller's bounds
    self.pageViewController.view.frame = self.view.bounds;

    [self.pageViewController didMoveToParentViewController:self];

    // Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
    self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;

}