【问题标题】:Why don't UISupportedInterfaceOrientations settings affect UIViewController behavior?为什么 UISupportedInterfaceOrientations 设置不影响 UIViewController 的行为?
【发布时间】:2011-09-12 03:41:51
【问题描述】:

Xcode (4) 突出显示了 UISupportedInterfaceOrientations 设置,但无论我如何设置它们,它似乎都不会影响基本应用程序的功能。 Xcode 模板插入注释掉了以编程方式报告支持的实现:

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

根据我的经验,此代码与应用程序的捆绑设置无关。因此,我编写了以下代码来根据应用设置自动报告对方向的支持。

为什么这不是默认实现?我错过了什么吗?这段代码不是必需的吗? (任何人都可以提出任何改进建议?)我可能会将其变成 UIViewController 的一个类别

/**
 * This implementation coordinates the app's orientation support with the app
 * bundle settings, simplifying the configuration of the app's support for its
 * different orientations. 
 */
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    static NSString *_orientationFlagNames[] = {
        Nil,                                        // UIDeviceOrientationUnknown
        @"UIInterfaceOrientationPortrait",          // UIDeviceOrientationPortrait,
        @"UIInterfaceOrientationPortraitUpsideDown",// UIDeviceOrientationPortraitUpsideDown,
        @"UIInterfaceOrientationLandscapeRight",    // UIDeviceOrientationLandscapeLeft [sic]
        @"UIInterfaceOrientationLandscapeLeft"      // UIDeviceOrientationLandscapeRight [sic]
                                                    // UIDeviceOrientationFaceUp
                                                    // UIDeviceOrientationFaceDown
    };

    NSArray *supportedOrientation = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];

    BOOL shouldRotate = (interfaceOrientation < sizeof(_orientationFlagNames)/sizeof(NSString*)
        && [supportedOrientation containsObject:_orientationFlagNames[interfaceOrientation]]);
    return shouldRotate; 
}

【问题讨论】:

    标签: ios uiviewcontroller bundle resourcebundle


    【解决方案1】:

    我认为这里有区别。 info.plist 值表示应用支持的方向,而shouldAutorotateToInterfaceOrientation: 表示特定视图可用的方向。

    如果你看here,很明显info.plist 值帮助iOS 选择初始方向来启动应用程序。

    【讨论】:

    • 您的链接显示,“...指定您的应用程序支持的界面方向。系统使用此信息(连同当前设备方向)来选择启动应用程序的初始方向。”我只阅读了声明的第一部分,但我想这确实是在说设置指示了初始支持的方向。我的解释正确吗?
    • 它是这样读取的,但事实并非如此 - 如果您没有在此处指定所需的所有方向,您的应用程序将不会旋转到它们,即使在启动后也是如此。如果我错了,请有人纠正我。
    猜你喜欢
    • 2017-10-29
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 2013-06-12
    相关资源
    最近更新 更多