【问题标题】:preferredInterfaceOrientationForPresentation must return a supported interface orientationpreferredInterfaceOrientationForPresentation 必须返回受支持的界面方向
【发布时间】:2012-10-02 13:14:05
【问题描述】:

此错误没有意义,因为首选方向 UIInterfaceOrientationLandscapeRight 由支持的方向返回

//iOS6

-(BOOL)shouldAutorotate
{
    return NO;
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

错误:

由于未捕获的异常而终止应用程序 'UIApplicationInvalidInterfaceOrientation',原因: 'preferredInterfaceOrientationForPresentation 必须返回一个受支持的 界面方向!'

【问题讨论】:

    标签: ios cocoa ios6


    【解决方案1】:

    您的代码应如下所示:

    -(BOOL)shouldAutorotate
    {
        return NO;
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscape;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return UIInterfaceOrientationLandscapeRight;
    }
    

    此外,请确保在您的Info.plist 中为您的应用设置了正确的方向,因为您从supportedInterfaceOrientations 返回的内容与Info.plist 相交,如果找不到共同的方向,那么您将得到那个错误。

    【讨论】:

    • 我发现这让我很伤心!我有一个通用应用程序共享视图控制器代码,并使用上述代码测试用户习语。 ipad 只需要横向,所有需要横向的条的 iphone 纵向。我无法在处提供正确的方向
    • 请注意,“UIInterfaceOrientationMaskLandscape”的“Mask”部分是此答案的重要部分。原始发帖人在他的方法中使用了错误的枚举。 Apple 为这种方法创建了一组新的枚举/选项,导致人们犯这个简单的错误,这似乎有点愚蠢——此外,Xcode 甚至不提供任何编译器时间检查,因为该方法返回 NSUInteger。
    • @lms,我的整个应用程序应该只支持纵向模式,除了一个视图(需要支持横向)。在 Plist 中,我只为纵向设置了方向,并且我在上面的代码中编写了我想更改横向方向的位置。但它给出了 UIInterfaceOrientationLandscapeRight 或 UIInterfaceOrientationLandscapeLeft。但我想两者都在我的观点中。你能告诉我我怎么能得到它。
    【解决方案2】:

    supportedInterfaceOrientations 仅在 shouldAutorotate 设置为 YES 时被调用

    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscape;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return UIInterfaceOrientationLandscapeRight;
    }
    

    对我来说最简单的方法就是设置 Info.plist

    如果您想支持 iOS 5,请在您的视图控制器中使用此代码。

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }
    

    【讨论】:

      【解决方案3】:

      这些是 supportedInterfaceOrientations 的错误枚举。您需要使用UIInterfaceOrientationMaskLandscapeLeft等(注意中间的单词掩码

      【讨论】:

        【解决方案4】:

        来自文档:

        -(NSUInteger)supportedInterfaceOrientations {
        
            return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; 
        }
        

        注意正确的方向是“面具”! 你试过了吗?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-16
          • 2013-04-28
          • 2015-12-23
          • 1970-01-01
          • 1970-01-01
          • 2019-03-06
          • 2019-10-31
          • 2016-09-07
          相关资源
          最近更新 更多