preferredInterfaceOrientationForPresentation must

2019-06-25 07:55发布

问题:

My application window's root view controller is a subclassed UINavigationController. I have added this code to the class:

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

In my root UIViewController I have added this code:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait;
}

When the device is rotated to landscape on this view controller, I present a modal view controller. When the device is rotated back to portrait, I am supposed to dismiss the modal view, but when I do I get the following error:

'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'

Why am I getting this error?


I tried returning YES from shouldAutorotate in the root UIViewController, and now I get the error 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'. This makes no sense to my, because UIInterfaceOrientationPortrait is one of the applications supported orientations.

回答1:

In -supportedInterfaceOrientations, you need to return values from UIInterfaceOrientationMask, not UIInterfaceOrientation. In particular, it looks like you want UIInterfaceOrientationMaskPortrait

Here's what the documentation for -supportedInterfaceOrientations says about the return value:

Return Value

A bit mask specifying which orientations are supported. See “UIInterfaceOrientationMask” for valid bit-mask values. The value returned by this method must not be 0.