【问题标题】:UIPageViewController: how to handle different orientation for view controllers?UIPageViewController:如何处理视图控制器的不同方向?
【发布时间】:2016-02-22 20:32:32
【问题描述】:

我从 UIPageViewContoller 类继承了 3 个视图控制器。其中两个应该只有纵向方向和一个 - 所有方向。我添加了这样一段代码:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return self.currentStackVC.supportedInterfaceOrientations()
}

override func shouldAutorotate() -> Bool {
    return self.currentStackVC.shouldAutorotate()
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return self.currentStackVC.preferredInterfaceOrientationForPresentation()
}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

}

currentStackVC 是当前可见的视图控制器。 我的视图控制器中也有这样的代码,实现方式略有不同:

前两个视图控制器(下一个 - VC):

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait
}

override func shouldAutorotate() -> Bool {
    return false
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return UIInterfaceOrientation.Portrait
}

最后:

override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.All
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return UIInterfaceOrientation.Portrait
}

当我位于第三个视图控制器上时 - 我旋转了我的设备并应用了新的方向。但不幸的是,它也适用于其他 VC。系统不会在第一个和第二个 VC 中询问 shouldAutorotate()。 (我认为因为每次只问当前的第三个 VC)。我去第二个VC(使用滑动),它也是横向的。所以,我的问题是 - 如何在 Page VC 中为不同的屏幕处理不同的方向?

提前致谢

【问题讨论】:

  • 你解决了吗?
  • 不,我没有。

标签: ios swift2 uipageviewcontroller uiinterfaceorientation


【解决方案1】:

你应该使用 UIPageViewController 的委托方法

- (UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;

例如把这段代码放到 UIPageViewController 的委托中

- (UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:(UIPageViewController *)pageViewController {
    return [[pageViewController.viewControllers lastObject] supportedInterfaceOrientations];
}

并覆盖子 VC 中的方法。

VC1

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

VC2

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

【讨论】:

  • 对 Objective-C 感到抱歉。我觉得转换成swift很容易
  • 没关系,谢谢,我试试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
  • 1970-01-01
  • 2014-08-27
  • 1970-01-01
  • 2011-02-10
  • 2012-12-22
相关资源
最近更新 更多