在iOS 6上使用故事板处理方向/旋转

Joh*_*tin 5 cocoa orientation ios6

所以....我刚刚花了整整一个下午,看过各种各样的帖子,尝试了很多东西让它发挥作用,没有运气.我现在已经把这个简单地回到了尽可能简单的情况下,它似乎仍然没有用.

在这个简单的场景中,我有一个只包含UINavigationController和一个初始视图控制器的storyboard: 在此输入图像描述

看过有关IOS 6中自动旋转方法更改的所有帖子,我创建了UINavigationController的子类,并在这个子类和第一个视图控制器上实现了shouldAutorotate和supportedInterfaceOrientations方法:

导航控制器:

// NavController.h
#import <UIKit/UIKit.h>

@interface NavController : UINavigationController

@end

// NavController.m
#import "NavController.h"

@interface NavController ()

@end

@implementation NavController

- (BOOL) shouldAutorotate {
    return YES;
}

- (NSUInteger) supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

@end
Run Code Online (Sandbox Code Playgroud)

第一控制器

// FirstController.h
#import <UIKit/UIKit.h>

@interface FirstController : UIViewController

@end

// FirstController.m
#import "FirstController.h"

@interface FirstController ()

@end

@implementation FirstController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

@end
Run Code Online (Sandbox Code Playgroud)

项目设置指定应支持所有旋转: 在此输入图像描述

这些似乎都没有任何影响......!当我在模拟器中运行时,我得到:

在此输入图像描述

在此输入图像描述

请..!我在这里错过了什么?这真让我抓狂!我想至少让这个简单的案例工作,所以我希望能让我的相当大的应用程序工作!

Raz*_*van 11

如果您计划为所有视图控制器启用或禁用旋转,则不需要子类化UINavigationController.而是使用:

   -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
Run Code Online (Sandbox Code Playgroud)

在你的AppDelegate中.

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

   -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
Run Code Online (Sandbox Code Playgroud)

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

    - (BOOL)shouldAutorotate
Run Code Online (Sandbox Code Playgroud)

- (NSUInteger)supportedInterfaceOrientations
Run Code Online (Sandbox Code Playgroud)

但是如果您打算在同一个导航堆栈(例如我)中的不同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;
}
Run Code Online (Sandbox Code Playgroud)

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

  • 就是这个.这就是答案.每个人的问题的关键是UINavigationController是初始视图控制器,其他一切都只是它的孩子.我希望我在40分钟前就知道了. (2认同)

Joh*_*tin 1

感谢大家的建议……经过几个小时的挖掘,我终于发现了问题所在。

由于某种原因,即使在项目设置中我指定了支持的所有方向,Xcode 也没有正确更新应用程序的 Info.plist!

对于我创建的示例项目,Xcode 仅向支持的界面方向 (iPad) 字典添加了两个值:

 - Portrait (top home button)
 - Portrait (bottom home button)
Run Code Online (Sandbox Code Playgroud)

添加两个缺失的方向最终使应用程序正确旋转:

 - Landscape (left home button)
 - Landscape (right home button)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

不知道为什么这些设置不正确。多么令人沮丧的一天啊!

再次感谢你的帮助。