【问题标题】:View won't rotate after Navigation Controller is embedded嵌入导航控制器后视图不会旋转
【发布时间】:2013-04-24 18:21:41
【问题描述】:

我启动了一个示例项目并将以下代码添加到我的视图控制器中。启动后屏幕将是横向的。 然后我去我的故事板,点击我唯一的视图,-> 编辑器-> 嵌入-> 导航控制器。再次启动程序:屏幕将是纵向的,无论我尝试什么都不会变成横向。这是为什么? (所有方向都在我的 plist 中启用。)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

@end

【问题讨论】:

    标签: ios objective-c cocoa-touch


    【解决方案1】:

    在 iOS 6 中,规则是一旦你在 UINavigationController 中,this 视图控制器 (ViewController) 的 supportedInterfaceOrientations 就无关紧要了。唯一咨询的是:

    • 应用程序 (Info.plist)。

    • 应用委托 (application:supportedInterfaceOrientationsForWindow:)

    • 根视图控制器,在您的情况下现在是 UINavigationController

    现在,我不完全明白为什么你的 UINavigationController 给你一个只有肖像的结果。但我确实知道,如果你想控制 UINavigationController 对旋转所做的事情,你必须继承 UINavigationController,使这个导航控制器成为你的子类的一个实例,并将supportedInterfaceOrientations 代码放入你的子类。

    注意我尝试按照您说的做,在我的机器上,导航控制器确实对所有三个标准方向执行补偿旋转。所以我不明白你为什么说它只是肖像。然而,答案还是一样的。

    【讨论】:

    • 你是对的。在对其进行子类化后,我确实设法旋转到了横向,但是如果我将 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } 添加到 ViewController A 中,这个 ViewController 将是纵向的(而其他视图控制器将从横向开始)所以说“此视图控制器的 supportedInterfaceOrientations (ViewController)无关紧要”是正确的。我错了吗?
    • preferredInterfaceOrientationForPresentation 无关紧要。这不是“演示”情况。
    • 关于你的笔记。我在谈论视图启动的方向(不旋转设备)。我仍然没有完全解决我的问题,但我会为此提出另一个问题。谢谢。
    • 你开始的方向是plist中的第一个方向。
    • 如何强制一个 ViewController 以横向开始,而另一个 ViewController 以纵向开始?它们都像子类 NavigationController
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    相关资源
    最近更新 更多