iphone - 无法获得支持的界面方向数组,为什么?

标签 iphone ios ipad uiinterfaceorientation

在这段代码中:

   NSArray *supportedOrientations = nil;
   if( iPhone ) { // bool
        supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
        // one array element
   }
   else if( iPad ) { // bool
        supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations~ipad"];
        // returns nil
   }
   else {
        NSLog(@"%@:%@ device type not identified", kClassName, kMethodName);
   }

如果设备是 iPhone,supportedOrientations 有数组。

如果是 iPad,supportedOrientations 为零。

该文件总是被找到,因此 NSLog 永远不会显示(通过调试器逐步确认)。

使用文本编辑检查 plist 时,我看到:
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>

关于为什么会发生这种情况的任何想法?

在 iOS 模拟器 4.3 版中运行。

最佳答案

当一个包加载它的infoDictionary ,它检查字典中的每个键。如果 key 是特定于设备的,则捆绑包会检查该 key 是否引用当前设备。

  • 如果 key 引用当前设备,则捆绑包会删除 key 的特定于设备的部分。例如,在 iPad 上,捆绑更改 UISupportedInterfaceOrientations~iPadUISupportedInterfaceOrientations .
  • 如果 key 指向不同的设备,则捆绑包会从字典中删除 key 。例如,在 iPhone 上,捆绑包会删除 UISupportedInterfaceOrientations~iPad字典中的键。

  • 所以你可以这样做:
    NSArray *supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
    

    这将在所有设备上做正确的事情。

    你可以这样做,它会更短一点:
    NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
    

    关于iphone - 无法获得支持的界面方向数组,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8370166/

    相关文章:

    ios - 如何从数字键盘 Xcode 9 Swift 4 调用 textFieldShouldReturn

    javascript - three.js - 用于 ipad 的管几何结构上的 Canvas 纹理

    ios - 要动态添加 UIView,或创建 UITableView? IOS

    javascript - 用 JavaScript 模仿 iPhone 主屏幕滑动

    iphone - 使用 NSSetUncaughtExceptionHandler 在 Objective C 中注册 UncaughtExceptionHandler

    iphone - 下载图像时出现内存警告和应用程序崩溃

    iphone - Apple Push Notification——是否可以批量请求大量通知?

    ipad - iPad 中的 MPMoviePlayerController 全屏怪癖

    ios - Objective-c,有没有办法从 URL 获取视频缩略图而不是使用 MPMoviePlayerController 和 AVAssetImageGenerator?

    iphone - 如何让某些 View 不旋转?