【问题标题】:iPad Pro device detectioniPad Pro 设备检测
【发布时间】:2015-10-31 15:33:59
【问题描述】:

我正在尝试检测 iPad Pro 设备,并尝试通过以下方式猜测其高度:

NSLog(@"%f",self.view.frame.size.height);

但它返回 1024 !与 iPad 非视网膜设备相同。有什么建议吗?

我需要用这行代码为 iPad Pro 指定一些代码:

#define iPadPro ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && [UIScreen mainScreen].bounds.size.height == 2732)

...即使在 iOS 模拟器上,代码也必须检测到 iPad Pro!

谢谢

编辑: 有人建议使用 LunchScreen ,但是当我使用它时会发生这种情况(按比例缩小):

【问题讨论】:

  • 您不能使用启动屏幕。支持 iPad Pro 需要一个可以放大到更大尺寸的启动屏幕。
  • 您可以使用 UIScreen 的 -nativeBounds 方法来解决这个问题。但是,如前所述,如果 bounds 与常规 iPad 相同,则意味着您尚未声明对设备的本机支持(通过启动图像),因此它将假定应用程序面向常规 iPad 尺寸,并且只会让 -bounds 返回常规 iPad 的大小,然后缩放其所有绘图。因此,问题是您是否想知道它是否是 iPad Pro,或者您是否需要以 iPad Pro 的尺寸进行绘图。如果是后者,你所拥有的一切都很好。
  • 正如我所说,要支持 iPad Pro,您必须使用启动屏幕。您可以使用 iPad Pro 的启动图像。如果没有正确的启动屏幕,您的应用表明它支持 iPad Pro,并且会认为它是在较小的非 Pro iPad 上运行。
  • 你没有在听。为了让任何应用程序支持 iPad Pro,您的应用程序必须使用 LaunchScreen。您不能为 iPad Pro 使用启动图像。一旦您添加了正确的 LaunchScreen(将用于运行 iOS 8.0 及更高版本的所有设备),您的应用程序将不会在 iPad Pro 上进行缩放。然后您可以根据需要使用问题中的代码。
  • 你在哪里记录视图的框架? [UIScreen mainScreen].bounds 得到什么?

标签: ios objective-c ipad


【解决方案1】:

特别感谢@rmaddy

检测屏幕尺寸的正确方法是:

NSLog(@"%f",[UIScreen mainScreen].bounds.size.height);

现在,如果您的应用程序在Portrait 模式下运行,您可以简单地使用此代码来检测 iPad Pro:

#define iPadPro ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && [UIScreen mainScreen].bounds.size.height == 1366)

不要忘记使用 LaunchScreen 的必要性,否则应用程序将无法利用 iPad Pro 的大屏幕

【讨论】:

  • 不要忘记使用 LaunchScreen 的必要性,否则应用程序将无法利用 iPad Pro 的大屏幕。
  • iPad Pro 现在是 9.7 英寸,失败了
  • @jjxtra,它不会失败,因为高度点仍然保持在 9.7 英寸中的 1366。
  • @GeneCode 不符合这个:m.gsmarena.com/apple_ipad_pro_9_7-7984.php
  • 上述解决方案在 iPad pro 12.9 模拟器中不起作用
【解决方案2】:

无论设备的方向如何,都能检测到 iPad pro 12.9 英寸

#define iPadPro12 (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad && UIScreen.mainScreen.nativeBounds.size.height == 2732) 

【讨论】:

  • 上述解决方案在 iPad pro 12.9 模拟器中不起作用。在做: po [[UIScreen mainScreen] nativeBounds].size.height -> 它打印 2048 并在做 po [[UIScreen mainScreen] bounds].size.height -> 它打印 1024
【解决方案3】:

感谢@Mc.Lover

PortraitLandscape 方向的一些更新:

if (([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad && ([UIScreen mainScreen].bounds.size.height == 1366 || [UIScreen mainScreen].bounds.size.width == 1366))) {
//iPad Pro
}

【讨论】:

    【解决方案4】:

    这是一个无论设备方向如何都可以工作的检查:

    - (BOOL)isIpadPro
    {
        UIScreen *mainScreen = [UIScreen mainScreen];
        CGFloat width = mainScreen.nativeBounds.size.width / mainScreen.nativeScale;
        CGFloat height = mainScreen.nativeBounds.size.height / mainScreen.nativeScale;
        BOOL isIpad = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
        BOOL hasIPadProWidth = fabs(width - 1024.f) < DBL_EPSILON;
        BOOL hasIPadProHeight = fabs(height - 1366.f) < DBL_EPSILON;
        return isIpad && hasIPadProHeight && hasIPadProWidth;
    }
    

    请注意,这只适用于 iOS8+

    【讨论】:

    • 对于最后两个布尔值,第一个不应该是hasIPadProWidth,第二个是hasIPadProHeight吗?
    • 对我来说效果很好。我把它放在一个类别中作为 UIDevice 上的一个类方法。
    【解决方案5】:

    Swift 3+ 的固定解决方案

    有两种解决方案,第一种我在我的项目中使用过,你可以使用其中任何一种。

    1- 使用宏

    2- 使用扩展

    使用宏

    #define iPadPro129 (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad && 
                                      UIScreen.mainScreen.nativeBounds.size.height == 2732) 
    
    #define iPadPro105 (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad && 
                                      UIScreen.mainScreen.nativeBounds.size.height == 2224) 
    

    使用扩展

    extension UIDevice {
    
        // for ipad pro 12.9 device
         public var isPadPro129: Bool {
             if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad 
                 && UIScreen.main.nativeBounds.size.height == 2732) {
                  return true
             }
             return false
         }
    
    
    
      // for ipad pro 10.5 device
         public var isPadPro105: Bool {
             if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad 
                 && UIScreen.main. nativeBounds.size.height == 2224) {
                  return true
             }
             return false
         }
    
    }
    

    【讨论】:

      【解决方案6】:

      D1mers0n 解决方案的 Swift 版本。

      func isIpadPro() -> Bool
      {
          if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad &&
              (UIScreen.mainScreen().bounds.size.height == 1366 || UIScreen.mainScreen().bounds.size.width == 1366)) {
              return true
          }
          return false
      }
      

      【讨论】:

        【解决方案7】:

        遵循 Swift3 标准/实践,更好的方法是:

        extension UIDevice {
        
             public var isiPadPro12: Bool {
                 if UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad 
                     && (UIScreen.main.bounds.size.height == 1366 || UIScreen.main.bounds.size.width == 1366) {
                      return true
                 }
                 return false
             }
        
        }
        

        将此添加到UIDeviceextension 允许您调用,这很整洁:

        UIDevice.current.isiPadPro12

        总有一天,Apple 最终会给我们一个 enum 值!

        【讨论】:

          【解决方案8】:

          我在 Objective-C 中使用宏的解决方案:

          define IS_IPAD_DEVICE   ([(NSString *)[UIDevice currentDevice].model hasPrefix:@"iPad"])    
          define IS_IPAD_IDIOM    (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)    
          define IS_IPAD          (IS_IPAD_DEVICE || IS_IPAD_IDIOM)    
          define IS_PORTRAIT                              UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])    
          define IS_LANDSCAPE                             UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])    
          define IS_IPAD_PRO_12_9  (IS_IPAD && ((IS_PORTRAIT && [[UIScreen mainScreen] nativeBounds].size.height == 2732.0) || (IS_LANDSCAPE && [[UIScreen mainScreen] nativeBounds].size.height == 2048.0)))    
          define IS_IPAD_PRO_11    (IS_IPAD && ((IS_PORTRAIT && [[UIScreen mainScreen] nativeBounds].size.height == 2388.0) || (IS_LANDSCAPE && [[UIScreen mainScreen] nativeBounds].size.height == 1668.0)))    
          define IS_IPAD_PRO_10_5  (IS_IPAD && ((IS_PORTRAIT && [[UIScreen mainScreen] nativeBounds].size.height == 2224.0) || (IS_LANDSCAPE && [[UIScreen mainScreen] nativeBounds].size.height == 1668.0)))    
          define IS_IPAD_OR_MINI   (IS_IPAD && ((IS_PORTRAIT && [[UIScreen mainScreen] nativeBounds].size.height == 2048.0) || (IS_LANDSCAPE && [[UIScreen mainScreen] nativeBounds].size.height == 1536.0)))
          

          【讨论】:

            【解决方案9】:

            这不是最好的长期解决方案,但它今天对你有用...

            获取设备的硬件字符串...

            size_t size;
            sysctlbyname("hw.machine", NULL, &size, NULL, 0);
            char *machine = malloc(size);
            sysctlbyname("hw.machine", machine, &size, NULL, 0);
            
            NSString *hardwareString = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
            free(machine);
            

            将硬件字符串与这些硬件字符串进行比较:

            if ([hardwareString isEqualToString:@"iPad6,7"] || [hardwareString isEqualToString:@"iPad6,8"]) {
                // iPad Pro
            }
            

            如果你愿意,我有一个包含所有这些的课程,我可以发送给你。从来没有真正把它打磨得足以制作一个豆荚,但如果需要的话,我确实有它。

            【讨论】:

            • 不要这样做。有适当的方法来检测屏幕尺寸。
            • 我相信问题是你如何检测设备类型而不是屏幕尺寸。
            • 但检测设备类型的唯一原因(至少对于 OP 的问题)是根据屏幕尺寸以不同方式处理事情。
            • 我不是这样读的。听起来 Mc.Lover 想在 iPad Pro 上做一些特定的事情,使用屏幕尺寸作为衡量 iPad 与 Pro 的指标。无论哪种方式。如果您需要将产品推向市场,我的方法将适用于今天,但每次 Apple 发布新模型时都需要维护——硬件字符串需要更新。
            • 这是不使用此答案的另一个原因。它在模拟器中不起作用。添加对 iPad Pro(启动屏幕)的适当支持,原始代码将按预期工作。
            【解决方案10】:

            Justin Domnitz 的 D1mers0n 解决方案的 Swift 3 版本:

            func isIpadPro12() -> Bool
                {
                    if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad 
                    && (UIScreen.main.bounds.size.height == 1366 || UIScreen.main.bounds.size.width == 1366)) {
                        return true
                    }
                    return false
                }
            

            【讨论】:

            • 作为变量而不是函数会更好,检查我遵循 swift3 实践的方法
            【解决方案11】:

            这是我使用的,用于

            extension UIDevice {
            
                public class var isiPad: Bool {
                    return UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
                }
            
                public class var isiPadPro129: Bool {
                    return isiPad && UIScreen.main.nativeBounds.size.height == 2732
                }
            
                public class var isiPadPro97: Bool {
                    return isiPad && UIScreen.main.nativeBounds.size.height == 2048
                }
            
                public class var isiPadPro: Bool {
                    return isiPad && (isiPadPro97 || isiPadPro129)
                }
            
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-01-06
              • 1970-01-01
              • 2022-01-04
              • 1970-01-01
              • 2019-11-10
              • 1970-01-01
              相关资源
              最近更新 更多