UIPageViewController - first rotation resets views to initial page?

夙愿已清 提交于 2019-12-12 12:27:57

问题


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.



来源:https://stackoverflow.com/questions/14188592/uipageviewcontroller-first-rotation-resets-views-to-initial-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!