UIPageViewController - first rotation resets views

2019-07-08 00:10发布

问题:

I am trying to understand Apple's default template for a Page-based application. That template shows a sort of calendar with one page for each month of the year.

When compiling the template without any changes, I noticed something odd. When the app launches, it displays in portrait mode, and when - after flipping through some of the pages (say, to June) - you change the rotation, it reloads in landscape mode showing two pages, BUT STARTING WITH JANUARY. This happens only once, though. On subsequent orientation changes, it then jumps to the correct "currentViewController".

The code for this seems to come from the RootViewController file, specifically

UIViewController *currentViewController = self.pageViewController.viewControllers[0];

And frankly, it seems to be ignored for the first orientation change.

I am trying to understand why?

回答1:

I had to deal with the same issue when updating an App to be iPhone 5 friendly. When compile with iOS 6 sdk, the problem appear on iOS 6, but not on iOS 5. It happens only when entering the PageViewController from landscape orientation.

A workaround :

Create a property to save the current state in @interface section of your .m :

@property (strong, nonatomic) NSArray *viewControllersSave;

Save the viewControllers in willRotateToInterfaceOrientation :

(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    // some code here...
    self.viewControllersSave = self.pageViewController.viewControllers;
}

Use the saved property instead of the PageViewController property in spineLocationForInterfaceOrientation.

- (UIPageViewControllerSpineLocation)pageViewController: (UIPageViewController *)pageViewController
                   spineLocationForInterfaceOrientation: (UIInterfaceOrientation)orientation
{
   // some code
   NSArray *viewControllers = @[self.viewControllersSave[0]];
   [self.pageViewController setViewControllers:viewControllers
                            direction:UIPageViewControllerNavigationDirectionForward
                            animated:YES
                            completion:NULL];

return UIPageViewControllerSpineLocationMin;
}

This could be a possibility to avoid the problem BUT you should submit a bug report to Apple to avoid such a workaround for later usage.