25

我将新方法添加到我的代码中,如苹果 iOS 6 文档中所述,但在 iPhone 5 上,应用程序不会倒置。只有景观方式。

这是来自rootViewController的我的代码:

- (NSUInteger)supportedInterfaceOrientations{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
            UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
}
- (BOOL)shouldAutorotate{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    return YES;
}

我也试过“UIInterfaceOrientationMaskAll”但没有改变。奇怪的是,我的带有 iOS6 的 iPhone 4 确实使用相同的代码颠倒了。

有任何想法吗?

4

5 回答 5

61

UIInterfaceOrientationMaskAllIOS 6 使用iPad 和UIInterfaceOrientationMaskAllButUpsideDowniPhone的默认方向。如果您希望 iPhone 上的 ViewControllers 旋转到包括 UpsideDown 在内的所有方向,您必须添加 iOS 6 方法:

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

到所有视图控制器。如果您已完成此操作并且它不起作用,那么我打赌您使用的是 TabViewController 或 NavigationController,其中某些视图不会返回此内容。所有的 ViewController 都必须返回这个,以便它们中的任何一个来支持它。

或者,您可以将 Category 添加到作为您的 rootViewController 的 UITabViewController 或 NavigationController 类并覆盖该方法。你这样做:

@interface UITabBarController (RotationAll)
    -(NSUInteger)supportedInterfaceOrientations;
@end

@implementation UITabBarController (RotationAll)
-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}
@end

这些也是目前推荐的方法,现在在 Apple 开发者论坛中与 Apple 工作人员讨论,他们说早期不鼓励对 UINavigationController 进行子类化的文档已经过时,从 iOS 6 开始可以这样做。

于 2012-10-06T09:55:43.400 回答
12

感谢 Cliff Ribaudo 确实帮助您完成了基于 UITabbarController 的项目的答案,但在我的情况下,我只使用 UINavigationController 所以我修改了您对 UINavigationController 类别的答案。

@interface UINavigationController (RotationAll)
-(NSUInteger)supportedInterfaceOrientations;
@end   


@implementation UINavigationController (RotationAll)
  -(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
  }
@end
于 2013-04-26T10:02:44.627 回答
0

原谅冗长,但我认为您的答案如下。

我遇到了同样的问题,但它发生在包括我的 iphone 4 在内的所有 IOS 6 设备上。阅读你的问题实际上回答了我的问题。我很久以前就删除了这个,因为它没有做任何事情:

- (NSUInteger)supportedInterfaceOrientations{    
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
            UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown); }

我的代码在之前的 ios 版本中运行良好。看来这已成为 ios 6 中的强制要求。但仅适用于颠倒。其他方向工作正常。

所以谢谢。

回到你的问题。您是否查看过您的项目目标以查看是否所有“支持的界面方向”都是深灰色的?我想知道颠倒是否是浅灰色(即未选中)。我在快速测试中发现,在之前的 ios 版本中,这些也被忽略了。只要您从 shouldAutorotateToInterfaceOrientation 返回 YES,它将支持方向,但在 iOS 6 中不支持。

最后,您实现了 shouldAutorotate,我的代码中没有。我认为这默认为是。而且我认为这只是告诉操作系统尝试旋转,但是您必须告诉它您使用 shouldAutorotateToInterfaceOrientation 支持哪些方向。但是您的代码段并未显示您已经实现了 shouldAutorotateToInterfaceOrientation。我建议实现这一点,因为它告诉操作系统您支持哪些方向返回一个简单的“是”意味着您支持所有。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return YES;
}

希望这可以帮助。

于 2012-09-25T20:05:45.377 回答
0

默认目标设置是没有倒置方向,所以试试这个:

转到项目文件选择您的应用程序目标选​​择“Summery”选项卡检查所有方向类型是否已按下

祝你好运 :)

于 2012-10-09T17:57:01.803 回答
0

如果您打算为所有视图控制器启用或禁用旋转,则不需要继承 UINavigationController。而是使用:

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

在您的 AppDelegate 中。

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

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

从 AppDelegate 结合您的父视图控制器中的以下方法。

    - (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 控制呈现的视图控制器的旋转设置,因此您必须以某种方式拦截当前在导航堆栈中的视图控制器。所以这就是我所做的:)。希望有帮助!

于 2013-01-20T00:47:27.597 回答