【问题标题】:presentViewController not supporting orientation in iOS 6presentViewController 在 iOS 6 中不支持方向
【发布时间】:2012-09-24 14:09:55
【问题描述】:

我正在使用此代码

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);
}

请帮我解决这个问题。

提前致谢。

【问题讨论】:

  • 我在为 iOS 6 编译的应用程序中遇到了同样的问题;除 UIInterfaceOrientationPortraitUpsideDown 外,所有方向均有效;很奇怪。
  • @MrJre 任何与此相同的解决方案都非常奇怪。

标签: iphone ios uinavigationcontroller orientation ios6


【解决方案1】:

您必须在您的应用程序委托中包含此内容:

- (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's 不会查询其顶部视图控制器以获取支持的方向,尽管开发者论坛上的一位 Apple 工程师确实这么说......似乎它没有。因此,您应该为UINavigationController 添加一个类别,这是我使用的:

#import "UINavigationController+autorotate.h"

@implementation UINavigationController (autorotate)

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

@end

有关 AutoRotate 如何在 iOS 6 上工作的更多信息check out this answer

【讨论】:

  • UINavigationController 类别就足够了。
【解决方案2】:

你忘了:

- (BOOL)shouldAutorotate {
    return YES;
}

【讨论】:

  • 感谢您的回复。我添加了,但它不起作用,我还缺少任何其他东西。
  • 在支持的界面方向下的 Info.plist 中,是否列出了纵向和纵向颠倒?
【解决方案3】:

只需将 sn-p 复制粘贴到您的代码上方即可

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

@end

【讨论】:

    【解决方案4】:

    如果您打算为所有视图控制器启用或禁用旋转,则无需继承 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;
    }
    

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

    【讨论】:

    • 感谢@codeFi,您的建议解决了我的问题。在从 iOS5 迁移到 iOS6 时,我遇到了很多面向设备的问题。你让我开心!
    【解决方案5】:

    子类 UINavigationController 并像这样覆盖 shouldAutorotate 和 supportedInterfaceOrientations:

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

    现在你可以控制你的方向是每个 ViewController。只需覆盖每个 ViewController 中的两个方法即可。

    【讨论】:

      猜你喜欢
      • 2013-05-04
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 2013-01-03
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 2012-09-12
      相关资源
      最近更新 更多