iOS 6 Tab Bar App:shouldAutorotate无效

时间:2013-02-15 14:32:54

标签: ios6 uitabbarcontroller uiinterfaceorientation

我正在使用iOS 6和Xcode 4.5在Storyboard中使用标签栏和一些导航视图控制器开发应用程序

通常应用程序应支持所有界面方向,但我有两个视图,只应支持纵向模式。

所以我将以下代码添加到视图控制器中:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait;
}

在我开发的另一个应用程序上,iOS 6上没有故事板和导航视图控制器,但它不起作用! :/

我希望有人能提供帮助,因为我找到了其他一些无用的帖子......

来自德国的问候

Laurenz

修改

我也试过了 - 不行! :

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;

} 

2 个答案:

答案 0 :(得分:6)

据我所知,出现这个问题是因为UITabBarController和UINavigationController返回了自己的默认值 - (BOOL)shouldAutorotate和 - (NSUInteger)supportedInterfaceOrientations。

一种解决方案是通过类别(或仅仅是子类)扩展这两个类,以便在视图控制器中从这些方法的实现中返回适当的值。这对我有用(你可以把它放到你的App代表中):

@implementation UITabBarController(AutorotationFromSelectedView)

- (BOOL)shouldAutorotate {
    if (self.selectedViewController) {
        return [self.selectedViewController shouldAutorotate];
    } else {
        return YES;
    }
}

- (NSUInteger)supportedInterfaceOrientations {
    if (self.selectedViewController) {
        return [self.selectedViewController supportedInterfaceOrientations];
    } else {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

@end

@implementation UINavigationController(AutorotationFromVisibleView)

- (BOOL)shouldAutorotate {
    if (self.visibleViewController) {
        return [self.visibleViewController shouldAutorotate];
    } else {
        return YES;
    }
}

- (NSUInteger)supportedInterfaceOrientations {
    if (self.visibleViewController) {
        return [self.visibleViewController supportedInterfaceOrientations];
    } else {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}
@end

默认情况下,所有视图控制器都将继续自动旋转。在只应支持纵向模式的两个视图控制器中,请执行以下操作:

-(BOOL)shouldAutorotate {
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

答案 1 :(得分:0)

Jonathan的优秀答案。

我修改了他的代码,以便在单个代码段中处理导航控制器。

- (BOOL)shouldAutorotate {
    if (self.selectedViewController) {
        if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
            return [[[(UINavigationController*)self.selectedViewController viewControllers] lastObject] shouldAutorotate];
        }
        return [self.selectedViewController shouldAutorotate];
    } else {
        return YES;
    }
}

- (NSUInteger)supportedInterfaceOrientations {
    if (self.selectedViewController) {
        if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
            return [[[(UINavigationController*)self.selectedViewController viewControllers] lastObject] supportedInterfaceOrientations];
        }
        return [self.selectedViewController supportedInterfaceOrientations];
    } else {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
} 
相关问题