【问题标题】:How can you tell if a user is using an iPhone 6 or 6+如何判断用户使用的是 iPhone 6 还是 6+
【发布时间】:2014-09-26 22:10:11
【问题描述】:

如果这是一个非常愚蠢的问题,请原谅我,我只是所有这些东西的初学者。我想为 iPhone 6 和 6 Plus 制作一个单独的界面,但我不知道最好的方法是什么。这是一个非常简单的应用程序,只是为了让自己更熟悉语言等。我想知道是否有一种简单的方法可以判断用户是使用 6 还是 6 plus 来为他们制作单独的界面。谢谢:)

【问题讨论】:

  • 对所有 iPhone 使用一个情节提要。正确使用自动布局和约束,将东西放在正确的位置和大小。
  • 这篇文章也可能对你有所帮助:stackoverflow.com/questions/25756087/…

标签: ios objective-c iphone xcode


【解决方案1】:

使用单个故事板。从raywanderlich 学习autolayout,它认为这是一个非常好的资源。

另外,现在利用 xcode 6 中为 ios 8 引入的尺寸类。这称为自适应布局

因此,不要分别为 3.5、4、4.7、5.5 英寸设备制作上述四个视图,然后还要担心组合它们各自的横向模式,只需使用自动布局(如果可能的话)选择 4 个尺寸类别的组合,强烈推荐),并为将来可能出现的任何其他设备尺寸放松。

【讨论】:

    【解决方案2】:

    要检测 iPhone 版本请使用以下代码。

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
          if ([[UIScreen mainScreen] scale] == 2.0) {
    
               if([UIScreen mainScreen].bounds.size.height == 667){
                 // iPhone retina-4.7 inch(iPhone 6)
               } 
               else if([UIScreen mainScreen].bounds.size.height == 568){
                 // iPhone retina-4 inch(iPhone 5 or 5s)
               } 
               else{
                // iPhone retina-3.5 inch(iPhone 4s)
              }
          }
          else if ([[UIScreen mainScreen] scale] == 3.0)
          {
               if([UIScreen mainScreen].bounds.size.height == 736.0){
                  //iPhone retina-5.5 inch screen(iPhone 6 plus)
               }
          }
     }
    

    来自Here的参考

    【讨论】:

      【解决方案3】:

      将此代码写入 .pch 文件中

      #define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
      #define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
      

      只要您需要在任何控制器中,您只需检查条件即可。

      【讨论】:

        【解决方案4】:

        以下是按照您的要求执行的方法。需要注意的一件主要事情是在 iOS7 及更低版本中,当您检查 [[UIScreen mainScreen] bounds] 的屏幕边界时,无论手机处于何种方向,它总是将宽度和高度列为相同。因此,如果它是横向的 iPhone5模式下,它仍然会列出宽度为 320,高度为 568。在 iOS8 中,这种情况发生了变化,现在如果同一个 iPhone5 处于横屏状态,它将列出宽度为 568,高度为 320。以下是解决此问题的方法:

        + (BOOL) deviceHasFourInchScreen
        {
            return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:568.0];
        }
        
        + (BOOL) deviceHasFourPointSevenInchScreen
        {
            return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:667.0];
        }
        
        + (BOOL) deviceHasFivePointFiveInchScreen
        {
            return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:3.0 height:736.0];
        }
        
        + (BOOL) deviceHasScreenWithIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom scale:(CGFloat)scale height:(CGFloat)height
        {
            CGRect mainScreenBounds = [[UIScreen mainScreen] bounds];
            CGFloat mainScreenHeight;
        
            if ([OperatingSystemVersion operatingSystemVersionLessThan:@"8.0"])
            {
                mainScreenHeight = mainScreenBounds.size.height;
            }
            else
            {
                mainScreenHeight = (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) ? mainScreenBounds.size.width : mainScreenBounds.size.height;
            }
        
            if ([[UIDevice currentDevice] userInterfaceIdiom] == userInterfaceIdiom && [[UIScreen mainScreen] scale] == scale && mainScreenHeight == height)
            {
                return YES;
            }
            else
            {
                return NO;
            }
        }
        

        这里还有附带的 OperatingSystem 类方法:

        + (NSString *) currentOperatingSystemVersion
        {
            return [[UIDevice currentDevice] systemVersion];
        }
        + (BOOL) operatingSystemVersionLessThanOrEqualTo:(NSString *) operatingSystemVersionToCompare
        {
            return ([[self currentOperatingSystemVersion] compare: operatingSystemVersionToCompare options:NSNumericSearch] != NSOrderedDescending);    
        }
        

        【讨论】:

          【解决方案5】:

          在prefix.pch中加入以下行

          #define iPhoneVersion ([[UIScreen mainScreen] bounds].size.height == 568 ? 5 : ([[UIScreen mainScreen] bounds].size.height == 480 ? 4 : ([[UIScreen mainScreen] bounds].size.height == 667 ? 6 : ([[UIScreen mainScreen] bounds].size.height == 736 ? 61 : 999))))
          

          现在在编程中你可以说...

          if (iPhoneVersion==4) {
              NSLog("This is 3.5 inch iPhone - iPhone 4s or below");
          } else if (iPhoneVersion==5) {
              NSLog("This is 4 inch iPhone - iPhone 5 family");
          } else if (iPhoneVersion==6) {
              NSLog("This is 4.7 inch iPhone - iPhone 6");
          } else if (iPhoneVersion==61) {
              NSLog("This is 5.5 inch iPhone - iPhone 6 Plus.. The BIGGER");
          } else {
              NSLog("This is iPad");
          }
          

          但是我会说通过学习自动布局为所有 iPhone 使用一个故事板。

          【讨论】:

          • 使用switch 声明。否则,对于 iPad,你最终会评估那一大堆代码四次(每个if 语句一次)。并且代码在 iOS 8 下横屏时会失败。
          • 这是一个相当愚蠢的方法。当下一代 iPhone 发布并且代码错误地认为它是 iPad 时,它会崩溃。
          • @gnasher729 : 也让我知道你的更好的方法
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-28
          • 2012-04-01
          • 2012-01-27
          • 2015-07-06
          • 2020-10-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多