iOS 6的视图控制器在旋转,但不应该旋转。

8
我希望我的应用程序中有几个视图控制器在iOS 6.0中不旋转。 以下是我为使iOS 6中的旋转成为可能所做的内容:
1.) 在application:didFinishLaunchingWithOptions:中设置窗口的rootViewController。
self.window.rootViewController = self.tabBarController;

2.) 在我的目标(在XCode中)中设置“支持的界面方向”,以便我可以使用所有方向

3.) 实现了新的iOS 6.0旋转功能

- (BOOL) shouldAutorotate {

    return YES;
}


-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskAll;
}

4.) 由于某些原因,我创建了一个子类化的UINavigationController,并实现了一些新功能,并在原有的NavigationController之上使用了这个新的NavigationController。

到目前为止,一切都很好,所有的视图控制器现在都能够旋转到任何方向。现在我想让几个视图控制器保持竖屏不旋转。但是当我在这些特定的视图控制器中设置新的旋转方法时,它仍然会旋转到任何方向:

- (BOOL) shouldAutorotate {

    return NO;
}


-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskPortrait;
}

同时设置navigationController的旋转功能并不会改变任何东西。(所有的视图控制器都可以旋转到任何方向)

我做错了什么?

编辑:

同时设置首选接口方向也不会改变任何东西:

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {

    return UIInterfaceOrientationMaskPortrait;
}
3个回答

11

如果你想让我们所有的导航控制器都遵循顶部视图控制器,那么你可以使用一个分类(category)。我发现这比子类化更容易。

@implementation UINavigationController (Rotation_IOS6)

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

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

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

@end

5
注意这个解决方案——如果视图控制器的supportedInterfaceOrientations方法恰好调用导航控制器上的supportedInterfaceOrientations方法,那么就会发生无限递归。我曾经吃过苦头,因为苹果的UIPrintingProgressViewController会向导航控制器请求supportedInterfaceOrientations。 - tyler
@tyler 我们该怎么处理这个问题?Print结果导致无限递归。我无论如何都无法修复它。我在我的应用程序中使用了上述类别来旋转特定的视图控制器。 - Angad Manchanda
简而言之,不要使用分类,而是在UINavigationController的扩展中控制旋转。由于我们的应用程序中很少使用旋转,所以我最简单的做法是使用UINav类别来明确锁定旋转,然后创建一个UINav扩展,如果需要允许旋转的导航堆栈,则基本上执行上述类别所做的操作。 - tyler
@tyler,我创建了一个UINavigationController的子类并添加了一些方法,现在我应该在哪里使用这个子类呢?我想在我的应用程序中将几个视图控制器旋转到横向,并且在plist文件中我已经启用了(纵向和横向),同时还有PRINT工作。UIPrintingProgressViewController导致无限递归。我被卡住了。 - Angad Manchanda
@tyler 我明白了。对它进行子类化后,在Print上没有崩溃或无限递归的问题。谢谢。 - Angad Manchanda

0
你需要创建UITabBarController的类别以支持自动旋转。
.h文件的代码如下:
@interface UITabBarController (autoRotate)<UITabBarControllerDelegate>

    -(BOOL)shouldAutorotate;
    - (NSUInteger)supportedInterfaceOrientations;

@end

.m文件的代码如下:

-(BOOL)shouldAutorotate {

    AppDelegate *delegate= (AppDelegate*)[[UIApplication sharedApplication]delegate];
    return [delegate.tabBarController.selectedViewController shouldAutorotate];
}


- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

注意:AppDelegate的名称将会根据您项目中的AppDelegate文件名进行更改。


在Stack Overflow上,垃圾自我推广链接也是不受欢迎的。 - Andrew Barber

0
这对我来说有效:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

2
谢谢您的快速回答,但是这种方法在iOS 6中已经被弃用了。在iOS 6中,您应该使用两个方法shouldAutorotate和supportedInterfaceOrientations。 - NicTesla

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接