【问题标题】:UIPageViewController - first rotation resets views to initial page?UIPageViewController - 第一次旋转将视图重置为初始页面?
【发布时间】:2013-01-07 01:10:56
【问题描述】:

我正在尝试了解 Apple 针对基于页面的应用程序的默认模板。该模板显示了一种日历,一年中的每个月都有一个页面。

在不做任何更改的情况下编译模板时,我注意到了一些奇怪的事情。当应用程序启动时,它以纵向模式显示,并且当 - 在翻阅一些页面后(例如,到 6 月) - 您更改旋转时,它会以横向模式重新加载,显示两个页面,但从 1 月开始。不过,这只会发生一次。在随后的方向更改时,它会跳转到正确的“currentViewController”。

这方面的代码似乎来自 RootViewController 文件,具体来说

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

坦率地说,第一次方向改变似乎被忽略了。

我想知道为什么?

【问题讨论】:

  • 有趣!这只发生在 iOS6 中。在 5.1 模拟器中运行时,完全相同的代码可以正确执行。我设置了一个断点,并注意到在 5.1 中,函数 - (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation 在 UIPageviewcontrol 首次加载时不会执行,但在 6.0 中会执行。
  • 谢谢,我在发帖之前已经看过这个帖子,但这与我想发的内容无关。我正在使用来自 Apple 的基于默认页面的模板,没有任何更改,并且旋转错误仅发生在第一次方向更改时(并且仅在 iOS6 中)。它适用于随后的方向变化。

标签: xcode ios6 uipageviewcontroller


【解决方案1】:

在将应用程序更新为对 iPhone 5 友好时,我不得不处理同样的问题。使用 iOS 6 sdk 编译时,iOS 6 会出现问题,iOS 5 不会出现此问题。仅在横向进入 PageViewController 时才会出现。

解决方法:

创建一个属性以将当前状态保存在 .m 的 @interface 部分中:

@property (strong, nonatomic) NSArray *viewControllersSave;

将 viewControllers 保存在 willRotateToInterfaceOrientation 中:

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

使用保存的属性而不是spineLocationForInterfaceOrientation 中的PageViewController 属性。

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

这可能是避免问题的一种可能性,但您应该向 Apple 提交错误报告以避免这种变通方法以供以后使用。

【讨论】:

  • 谢谢!!!它并没有为我开箱即用,因为我一直得到 SIGABRT,直到我将代码从 willRotateToInterfaceOrientation 移动到 shouldAutorotateToInterfaceOrientation。我也稍微改变了它,而不是保存到数组中,我声明@property (strong, nonatomic) UIViewController *viewControllerSave; 并保存self.viewControllerSave = self.pageViewController.viewControllers[0];,这有利于在横向和纵向中使用我的spineLocationForInterfaceOrientation代码。我接受你的回答,因为它为我指明了正确的方向!
  • 感谢您的接受,除了计算supportedInterfaceOrientations 中支持的方向的代码之外,您不应执行任何其他代码。
  • 为什么不呢?来自 Apple 文档:developer.apple.com/library/ios/#featuredarticles/… - 使用轮换通知来更新应用的状态信息。如果您的应用使用当前方向来确定如何呈现内容,请使用视图控制器的旋转方法(或相应的设备方向通知)来记录这些更改并进行必要的调整。 就像我说的,代码你建议在willRotateToInterfaceOrientation中崩溃
  • 这个文档读到你应该使用通知和/或旋转方法(比如 willRotateFromInterfaceOrientation / didRotateRotateFromInterfaceOrientation...)没有具体的理由不这样做,这只是为了更好的代码设计
猜你喜欢
  • 2013-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多