frankWhite
frankWhite

Reputation: 1542

UIPageViewController: how to handle different orientation for view controllers?

I have inherited from UIPageViewContoller class with 3 view controllers. Two from them should have only Portrait orientation and one - all orientations. I added such piece of code:

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)

}

where currentStackVC is current visible view controller. Also I have such code in my view controllers with a little different implementations:

First two viewcontrollers(next - VC):

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

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

Last:

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

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

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

When I located on third view controller - I rotate my device and new orientation applied. But unfortunately it also applied for other VCs. System don't ask shouldAutorotate() in first and second VCs. (As I think because everytime asked only current 3rd VC). I go to second VC(using swipe) and its also in landscape orientation. So, my question is - how can I handle different orientations in Page VC for different screens?

Thanks in advance

Upvotes: 2

Views: 652

Answers (1)

you should use delegate methods of UIPageViewController

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

For example put this code into UIPageViewController's delegate

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

And override methods in child VCs.

VC1

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

VC2

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

Upvotes: 1

Related Questions