为什么 UISupportedInterfaceOrientations 设置不影响 UIViewController 的行为?

发布于 2024-11-14 22:23:40 字数 1840 浏览 3 评论 0原文

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; 
}

Xcode (4) highlights the UISupportedInterfaceOrientations settings, but no matter how I set them, it does not appear to affect a basic app's functionality. The Xcode templates insert commented out implementation for programmatically reporting support in:

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

In my experience, this code has nothing to do with the app's bundle settings. So, I wrote the following code to automatically report support for orientation based on the app settings.

Why isn't this the default implementation? Did I miss something and this code shouldn't be necessary? (Any improvements anyone can suggest?) I might turn this into a category for 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; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里°也失望 2024-11-21 22:23:41

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

如果您查看 < code>这里,明确提到info.plist值帮助iOS选择启动的初始方向 应用。

I think there is a distinction here. The info.plist value indicates the orientations that the app supports whereas the shouldAutorotateToInterfaceOrientation: indicates the orientations a particular view is available in.

If you look here, it is clearly mentioned that the info.plist values help iOS choose the initial orientation to launch the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文