在iOS 6中,presentViewController不支持方向问题。

13

我正在使用这段代码

if ([self respondsToSelector:@selector(presentViewController:animated:completion:)])
    {
        [self presentViewController:navigationControllerCustom animated:YES completion:nil];
    }
    else
    {
        [self presentModalViewController:navigationControllerCustom animated:YES];
    }

我的应用有两个方向:竖屏和倒置竖屏。这段代码在iOS 5.1上运行良好,但是在iOS 6上方向不起作用。

我还在我的navigationControllerCustom类中添加了这段代码。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

-(NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}

请帮我解决这个问题。

先行致谢。


2
我在为iOS 6编译的应用程序中遇到了同样的问题;除了UIInterfaceOrientationPortraitUpsideDown之外,所有方向都可以正常工作;这真是奇怪。 - MrJre
@MrJre,有没有类似的解决方案,这真的很奇怪。 - P.J
5个回答

25

你必须将以下代码包含在你的应用委托中:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

同时确保视图控制器均具备以下内容,这对我来说很有效。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
文档还说,UINavigationController 不会查询其顶部视图控制器所支持的方向,尽管在开发者论坛上的一位苹果工程师确实这样说过...但现在看来它并没有这么做。因此,您应该为UINavigationController添加一个类别,这是我使用的类别:
#import "UINavigationController+autorotate.h"

@implementation UINavigationController (autorotate)

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

@end

要了解iOS 6上的AutoRotate工作原理,请查看这个答案


1
UINavigationController类别已经足够。 - Kudit

2

您忘记了:

- (BOOL)shouldAutorotate {
    return YES;
}

谢谢您的回复。我已经添加了,但它没有起作用,还有其他我遗漏的东西吗? - P.J
在您的 Info.plist 文件中,支持的界面方向下,是否列出了纵向和倒置的纵向? - rocky

1

继承 UINavigationController 并覆盖 shouldAutorotate 和 supportedInterfaceOrientations 方法,代码如下:

-(BOOL)shouldAutorotate
{
    return self.topViewController.shouldAutorotate;
}

-(NSUInteger)supportedInterfaceOrientations
{
     return self.topViewController.supportedInterfaceOrientations;
}

现在您可以控制每个视图控制器中的方向。只需覆盖每个视图控制器中的两种方法即可。

1
如果您计划为所有视图控制器启用或禁用旋转,则不需要子类化UINavigationController。 相反,使用以下内容即可:
   -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

在您的AppDelegate中。

如果您计划在应用程序中支持所有方向,但父级视图控制器(例如UINavigationController堆栈)上的方向不同,则应使用

   -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

与您的父视图控制器中以下方法相结合使用。

    - (BOOL)shouldAutorotate

并且

- (NSUInteger)supportedInterfaceOrientations

但是,如果您计划在同一导航堆栈中的不同CHILDREN ViewControllers中拥有不同的方向设置(就像我一样),则需要检查导航堆栈中的当前ViewController。

我在我的UINavigationController子类中创建了以下内容:

    - (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    int interfaceOrientation = 0;

    if (self.viewControllers.count > 0)
    {
        id viewController;
        DLog(@"%@", self.viewControllers);
        for (viewController in self.viewControllers)
        {
            if ([viewController isKindOfClass:([InitialUseViewController class])])
            {
                 interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
            }
            else if ([viewController isKindOfClass:([MainViewController class])])
            {
                 interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
            }
            else
            {
                 interfaceOrientation = UIInterfaceOrientationMaskAllButUpsideDown;
            }
        }
    }
    return interfaceOrientation;
}

由于您无法再从子视图控制器中控制所呈现的视图控制器的旋转设置,因此必须以某种方式拦截当前导航堆栈中的视图控制器。这就是我所做的 :)。希望能对您有所帮助!


谢谢@codeFi,你的建议解决了我的问题。在从iOS5迁移到iOS6时,我一直在为各种设备方向问题而苦苦挣扎。你让我感到非常开心! - iOS-Coder

1

只需将该片段复制粘贴到您的代码上方

@implementation UINavigationController (rotation)
//temp hack for iOS6, this allows passing supportedInterfaceOrientations to child viewcontrollers.
- (NSUInteger)supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];
}

@end

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