iPhone Interface Orientation on iOS8

2019-08-03 23:30发布

问题:

I have been trying to do this for 2 weeks, and just found out the reason it's not working:

Deprecated APIs

The following APIs are deprecated:

The UIViewController methods and properties for interface orientation. Traits and size classes replace them, as described in Unified Storyboards for Universal Apps.

From what's new in iOS 8

What I need is: a FirstViewController, which is the rootViewController to my navigationController. The FristViewController should be only available in Portrait mode (not ever ever ever displayed in Landscape).

Then there are a few intermediate ViewControllers in the navigation stack (which support both orientations), until I reach a LastViewController, which should be available only in LandscapeRight mode (and never ever ever in portrait, or another mode).

I have been trying with the following CustomNavigationController, but apparently things changed in iOS8, and I can't get it to work:

- (BOOL)shouldAutorotate { // Available in iOS 6.0 and later
    return YES; //  // May use topViewController's, but in this app it always returns YES
}

- (NSUInteger)supportedInterfaceOrientations { // Available in iOS 6.0 and later
    if (self.topViewController != nil)
        return [self.topViewController supportedInterfaceOrientations];
    else
        return [super supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { // Available in iOS 6.0 and later
    if (self.topViewController != nil)
        return [self.topViewController preferredInterfaceOrientationForPresentation];
    else
        return [super preferredInterfaceOrientationForPresentation];
}

Thanks!

回答1:

The problem you are having has nothing whatever to do with iOS 8. Here are some points to note:

  • You have misunderstood the note about what's deprecated. Only methods with names like willRotate are deprecated, and you are not using them anyway.

  • Nothing has changed with regard to how supportedInterfaceOrientations works. Make sure you test with beta 4, though, because there was a bug in betas 1-3 that prevented presented view controller orientations from working correctly.

  • "Then there are a few intermediate ViewControllers in the navigation stack (which support both orientations), until I reach a LastViewController, which should be available only in LandscapeRight"... That is impossible, but not because of iOS 8. What you are describing has been illegal since iOS 6! You cannot have different forced orientations for different view controllers in a navigation stack. Only a presented view controller can force rotation (as I have explained here and in many other answers: https://stackoverflow.com/a/21616025/341994).



回答2:

Implement - (NSUInteger)supportedInterfaceOrientations appropriately in each of the view controllers in the navigation stack. It does not matter what the UINavigationController's supported orientations are. It ought to respect the supported orientations of its displayed view controller.

Also make sure that the all the necessary orientations are checked in your target's "General -> Deployment Info" configuration section.