3

I've spend hours on this and can't get it to work. Hope that someone can help me.

I have a UIPageViewController which works perfectly when I add it to my default view when the application starts. This is what I do:

//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];

Now I want to use a navigation controller in my initial view which then pushes the UIPageViewController on the stack, when the user clicks on a button. This works too, but when I'm in landscape mode the UIPageViewController shows only one page (which is horizontally streched) and not two, as it should be. I need to rotate the device to portrait and then back to landscape mode to force the UIPageViewController to show both pages. I'm adding the UIPageViewController to the navigation controller with:

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

When i debug my code I see that without the navigation controller the delegate methods of the UIPageViewController are called on startup. When I push the UIPageViewController on the navigation controller they're not called until I rotate the device.

Does anyone know how to solve this? Thanks in advance for any help/tips on this.

Pikebu
  • 101
  • 2
  • 6

1 Answers1

3

Figured this out with help from this thread.

The key is setting the spineLocation on the pageViewController when you create it through the options dictionary:

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

And this option should be put inside of a check to see what the current interface orientation is.

In the above mentioned thread, the interface orientation is checked by a ternary operator; I put it in an if-statement so that I can use the same opportunity to tack an extra UIViewController onto the viewControllers array.

If the interface orientation is portrait, 'options' will still be nil when the pageViewController is created, and so it'll go on to create it in portrait mode with only one viewController.

Here's my whole viewDidLoad code in BookViewController:

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

}
Community
  • 1
  • 1
jankins
  • 670
  • 5
  • 16