preferredInterfaceOrientationForPresentation must

2019-01-06 18:04发布

This error doesn't make sense, as the preferred orientation UIInterfaceOrientationLandscapeRight is returned by the supported orientation

//iOS6

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft);
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

Error :

Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'

标签: ios cocoa ios6
4条回答
来,给爷笑一个
2楼-- · 2019-01-06 18:15

supportedInterfaceOrientations is only called, if shouldAutorotate is set to YES

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

The easiest approach for me, is only to set the Info.plist

info.plist

If you like to support iOS 5 use this code in your view controllers.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
查看更多
萌系小妹纸
3楼-- · 2019-01-06 18:17

from the documentation:

-(NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; 
}

Note that the correct orientation is "Mask"! Did you try this?

查看更多
走好不送
4楼-- · 2019-01-06 18:28

Those are the wrong enums for supportedInterfaceOrientations. You need to use UIInterfaceOrientationMaskLandscapeLeft, etc (note the word mask in the middle)

查看更多
5楼-- · 2019-01-06 18:30

Your code should look like this:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

Also, make sure in your Info.plist you have set the correct orientations for you app because what you return from supportedInterfaceOrientations is intersected with the Info.plist and if it can't find a common one then you'll get that error.

查看更多
登录 后发表回答