【问题标题】:UINavigationController inside a UITabBarController won't rotate in iOS 6UITabBarController 中的 UINavigationController 在 iOS 6 中不会旋转
【发布时间】:2013-04-08 09:45:40
【问题描述】:

我在 UITabBarController 中有一个 UINavigationController。我尝试了社区关于 iOS 6 中自动旋转的所有建议,但没有成功,最后我决定为 UINavigationController 创建一个类别,该类别没有对 orientaion 进行任何更改(尽管确实调用了函数)

然后我为 UITabBarController 创建了一个类别,如下所示:

#import "UITabBarController+ios6Rotate.h"

@implementation UITabBarController (ios6Rotate)

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortraitUpsideDown;;
}

@end

得到了这个:

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

但是我所有的方向都支持?!嗯

然后我把代码改成这样:

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortraitUpsideDown;
}

这使我的应用程序颠倒了,但仍然不会旋转。我不明白发生了什么事。在这一点上,我想在 ios6 中看到任何形式的轮换,我不在乎哪一边,但似乎没有任何效果。

【问题讨论】:

    标签: ios objective-c cocoa-touch cocoa


    【解决方案1】:

    您应该确保添加正确的supportedInterfaceOrientations。您可以尝试在您的类别中调用相应的viewController 的定位方法。

    例如在UINavigationController 类别中会是这样的

    -(BOOL)shouldAutorotate
    {  
      return   [self.topViewController shouldAutorotate];   
    }
    
    -(NSUInteger)supportedInterfaceOrientations
    {
       return [self.topViewController supportedInterfaceOrientations];
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
       return [self.topViewController preferredInterfaceOrientationForPresentation];
    }
    

    【讨论】:

    • 我的 UINavigationController 类别中有完全相同的代码,但没有帮助(调用了函数但没有更改排序)。知道如何在 UITabBarController 类别上实现它吗?
    • 你应该在你的 UITabBarController 类别中获取 visibleController 并调用 viewcontrollers 的相应方法。
    • 什么 visibleController 和我应该调用什么方法?我真的不明白
    • 在navigationController的情况下,你可以调用相应的viewController的方法,因为viewController被推到navigationController里面,尝试TabBarController的方法,就像你以前做的那样。我假设你在其他地方有问题。你的方向有问题。
    【解决方案2】:

    我花了几个小时才弄明白。这是解决方案: 出于某种原因,previos 程序员做了这样的事情:

     [window addSubview:[someController view]];  // In the appDelegate
    

    我需要做的就是用这个替换它:

      [self.window setRootViewController:someController];
    

    自动旋转方向重新开启!

    【讨论】:

    • 是的 rootViewController 是 iOS6 的方式。你应该已经从控制台上的警告中弄清楚了。
    • 谢谢! - 旧方法可能是为了向后兼容 iOS 3.1,它还没有 'setRootViewController' 选择器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 2010-11-06
    • 2010-11-03
    • 2013-02-14
    • 2010-11-14
    • 1970-01-01
    相关资源
    最近更新 更多