【问题标题】:How can I detect whether a user has an iPhone 6 Plus in standard or zoomed mode?如何检测用户的 iPhone 6 Plus 是否处于标准模式或缩放模式?
【发布时间】:2014-11-15 03:07:27
【问题描述】:

如何检测用户的 iPhone 6 Plus 是标准模式还是缩放模式? 这可能吗?

我试过[UIScreen mainScreen].scale,两种情况下都报告3.0

【问题讨论】:

    标签: ios objective-c iphone iphone-6-plus uiscreen


    【解决方案1】:

    [UIScreen mainScreen].currentMode 报告:

    <UIScreenMode: 0x17802f240; size = 1242.000000 x 2208.000000> // STANDARD
    <UIScreenMode: 0x178226be0; size = 1125.000000 x 2001.000000> // ZOOMED
    

    【讨论】:

      【解决方案2】:

      有一个新成员

      [[UIScreen mainScreen] nativeScale]
      

      应该做你想做的事。它只在 iOS 8 上可用,所以你需要保护它

      【讨论】:

      • 这对我有用!只需添加 mainScreen()。 “UIScreen.mainScreen().nativeScale”。通过宽度和高度从 Web 服务获取图像并进入图像视图非常有用:D
      【解决方案3】:

      以下代码可用于获取boundscoordinateSpacenativeScalescale,即在 iPhone 6 Plus 上,nativeScale 为 2.608,当设备在缩放模式下运行时为2.88(注意在模拟器中不一样):

      UIScreen *mainScreen = [UIScreen mainScreen];
      NSLog(@"Screen bounds: %@, Screen resolution: %@, scale: %f, nativeScale: %f",
                NSStringFromCGRect(mainScreen.bounds), mainScreen.coordinateSpace, mainScreen.scale, mainScreen.nativeScale);
      

      检测 iPhone 6 Plus 的代码:

      -(BOOL)iPhone6PlusDevice{
          // Scale is 3 currently only for iPhone 6 Plus
          if ([UIScreen mainScreen].scale > 2.9) return YES;
          return NO;
      }
      

       -(BOOL)iPhone6PlusUnZoomed{
              if ([self iPhone6PlusDevice]){
                  if ([UIScreen mainScreen].bounds.size.height > 720.0) return YES;  // Height is 736, but 667 when zoomed.
              }
              return NO;
          }
      

      注意:如果您正在检查 iPhone 6 Plus,要调整用户界面,请不要依赖.nativeScale,因为模拟器和实际设备会给出不同的结果。

      【讨论】:

      • 我正在新的消息应用程序中运行我的扩展程序,该应用程序已针对 iOS8 中的新屏幕尺寸(例如未“缩放”的屏幕尺寸)、screenScale: 3.000000 和 @987654332 进行了适当更新@
      • @barfoon 报告的屏幕分辨率是多少?因为我住在一个没有 Apple Store 的国家,所以我无法购买新的 iPhone 6 Plus。
      • nativeScale 与应用程序是否在缩放模式下运行无关:stackoverflow.com/questions/25871858/…
      • @HHHH 你的权利,我已经更新了文本以反映尽管代码仍然有效。
      【解决方案4】:

      这些选项用于检测 iPhone 设备。

      #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
      #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
      #define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0)
      #define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
      #define IS_IPHONE_6PLUS (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f)
      #define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
      #define IS_RETINA ([[UIScreen mainScreen] scale] == 2.0) 
      

      【讨论】:

      • 自从 iOS 8 [UIScreen mainScreen].bounds.size 变得依赖于方向并且这些宏在横向模式下被破坏。可以通过使用 MAX(width, height) 进行值比较来修复。请在下面查看我的答案。
      【解决方案5】:

      从 Paula Chavarría 的 answer 更新了适用于 iOS 8 的宏(其中 [UIScreen mainScreen].bounds.size 取决于方向):

      #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
      #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
      #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
      #define IS_IPHONE_5 (IS_IPHONE && (MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 568.0) && ((IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale) || !IS_OS_8_OR_LATER))
      #define IS_STANDARD_IPHONE_6 (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 667.0  && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale)
      #define IS_ZOOMED_IPHONE_6 (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 568.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale > [UIScreen mainScreen].scale)
      #define IS_STANDARD_IPHONE_6_PLUS (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 736.0)
      #define IS_ZOOMED_IPHONE_6_PLUS (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 667.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale < [UIScreen mainScreen].scale)
      #define IS_IPHONE_6 (IS_STANDARD_IPHONE_6 || IS_ZOOMED_IPHONE_6)
      #define IS_IPHONE_6_PLUS (IS_STANDARD_IPHONE_6_PLUS || IS_ZOOMED_IPHONE_6_PLUS)
      

      【讨论】:

        【解决方案6】:

        这是一个简单的解决方案:

        var isZoomed: Bool {
            return UIScreen.main.scale != UIScreen.main.nativeScale
        }
        

        【讨论】:

        • 很遗憾,它不起作用。 12 迷你缩放:缩放 3.0,nativeScale 3.515625 ✅ 12 迷你无缩放:缩放 3.0,nativeScale 3.0 ✅ SE 第一代缩放:缩放 2.0,nativeScale 2.0 ❌ SE 第一代无缩放:缩放 2.0,nativeScale 1.7066666666666668❌
        • @DarekCieśla 这是一个不错的发现。我没有完全测试所有设备。我认为这可能是第一代 iPhone SE 的错误,因为我测试过的所有其他设备都运行良好。
        • 我刚刚在 xcode 12(包括 SE 第 2 代)中的所有可用模拟器上进行了测试,并返回了正确的结果。我没有在 SE 1st Gen 上测试它,因为它不支持 iOS 14。
        • @Fouad SE1 确实支持 iOS14 [...]。上面的比较对于 SE1 返回 false,因为它是唯一不支持缩放模式的设备,因此它的 scale 与 nativeScale 始终相同。话虽如此,代码如果可以在所有设备上安全使用,谢谢@Starsky!
        • 不!模拟器使用设备返回不同的值!
        猜你喜欢
        • 1970-01-01
        • 2014-11-16
        • 1970-01-01
        • 2014-11-03
        • 2010-10-24
        • 1970-01-01
        • 2016-02-02
        相关资源
        最近更新 更多