处理iOS 6上使用故事板的方向/旋转问题

5
所以...我刚刚花了整个下午的时间,看了各种帖子并尝试了许多方法来使它工作,但没有成功。现在,我已经把它简化到最简单的情况,但仍然不能工作。
在这个简单的场景中,我有一个故事板,只包含一个UINavigationController和一个单一的初始视图控制器: enter image description here 看到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

第一个控制器

// 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

项目设置指定应支持所有旋转: enter image description here 然而,这似乎没有任何效果...当我在模拟器中运行时,会出现以下情况: enter image description here enter image description here 请问我错过了什么?这让我疯掉!我至少想让这个简单的例子工作,这样我才有希望使我的相当大的应用程序能够工作!

如果您有一个有用的答案(即使是您自己回答的),您应该将其标记为“已接受”。这将标记问题为已关闭。 - Artemix
3个回答

11

如果你计划启用或禁用所有视图控制器的旋转,那么你不需要子类化UINavigationController。 相反,使用以下方法:

- (BOOL)shouldAutorotate;

- (UIInterfaceOrientationMask)supportedInterfaceOrientations;

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;

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

在您的AppDelegate中。

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

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

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

    - (BOOL)shouldAutorotate

- (NSUInteger)supportedInterfaceOrientations

但如果您计划在同一导航栈中的不同子视图控制器中拥有不同的方向设置(就像我一样),则需要检查导航栈中的当前视图控制器。

我已在我的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;
}

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


2
就是这样。这就是答案。每个人的问题的关键是UINavigationController是初始视图控制器,其他所有东西都只是它的子视图。真希望我40分钟之前就知道了这一点。 - Enrico Susatyo

1

发布你的AppDelegate->didFinishLaunchingWithOptions方法。

它应该像这样:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//    // Override point for customization after application launch.
//    self.window.backgroundColor = [UIColor whiteColor];
//    [self.window makeKeyAndVisible];
    return YES;
}

1
谢谢大家的建议...经过数小时的挖掘,我终于找到了问题所在。
尽管在项目设置中我已经指定了所有方向都被支持,但由于某种原因,Xcode没有正确地更新应用程序的Info.plist文件!
对于我创建的示例项目,Xcode只向“Supported Interface Orientations (iPad)”字典中添加了两个值:
 - Portrait (top home button)
 - Portrait (bottom home button)

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

 - Landscape (left home button)
 - Landscape (right home button)

enter image description here

不知道为什么这些设置没有被正确地设置。真是让人沮丧的一天!

再次感谢您的帮助。


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