How to detect iPhone 5 (widescreen devices)?

前端 未结 24 1282
我寻月下人不归
我寻月下人不归 2020-11-22 00:50

I\'ve just upgraded to XCode 4.5 GM and found out that you can now apply the \'4\" Retina\' size to your view controller in the storyboard.

Now if I want to create a

相关标签:
24条回答
  • 2020-11-22 01:02

    I used hfossli's answer and translated it to Swift

    let IS_IPAD = UIDevice.currentDevice().userInterfaceIdiom == .Pad
    let IS_IPHONE = UIDevice.currentDevice().userInterfaceIdiom == .Phone
    let IS_RETINA = UIScreen.mainScreen().scale >= 2.0
    
    let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width
    let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height
    let SCREEN_MAX_LENGTH = max(SCREEN_WIDTH, SCREEN_HEIGHT)
    let SCREEN_MIN_LENGTH = min(SCREEN_WIDTH, SCREEN_HEIGHT)
    
    let IS_IPHONE_4_OR_LESS = (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
    let IS_IPHONE_5 = (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
    let IS_IPHONE_6 = (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
    let IS_IPHONE_6P = (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
    
    0 讨论(0)
  • 2020-11-22 01:03

    Here is the correct test of the device, without depending on the orientation

    - (BOOL)isIPhone5
    {
        CGSize size = [[UIScreen mainScreen] bounds].size;
        if (MIN(size.width,size.height) == 320 && MAX(size.width,size.height == 568)) {
            return YES;
        }
        return NO;
    }
    
    0 讨论(0)
  • 2020-11-22 01:05

    In Swift, iOS 8+ project I like to make an extension on UIScreen, like:

    extension UIScreen {
    
        var isPhone4: Bool {
            return self.nativeBounds.size.height == 960;
        }
    
        var isPhone5: Bool {
            return self.nativeBounds.size.height == 1136;
        }
    
        var isPhone6: Bool {
            return self.nativeBounds.size.height == 1334;
        }
    
        var isPhone6Plus: Bool {
            return self.nativeBounds.size.height == 2208;
        }
    
    }
    

    (NOTE: nativeBounds is in pixels).

    And then the code will be like:

    if UIScreen.mainScreen().isPhone4 {
        // do smth on the smallest screen
    }
    

    So the code makes it clear that this is a check for the main screen, not for the device model.

    0 讨论(0)
  • 2020-11-22 01:07

    This way you can detect device family.

        #import <sys/utsname.h>
        NSString* deviceName()
        {
            struct utsname systemInformation;
            uname(&systemInformation);
            NSString *result = [NSString stringWithCString:systemInformation.machine
                                                  encoding:NSUTF8StringEncoding];
            return result;
        }
    
        #define isIPhone5  [deviceName() rangeOfString:@"iPhone5,"].location != NSNotFound
        #define isIPhone5S [deviceName() rangeOfString:@"iPhone6,"].location != NSNotFound
    
    0 讨论(0)
  • 2020-11-22 01:08

    If the project is created using Xcode 6, then use the below mentioned code to detect the devices..

    printf("\nDetected Resolution : %d x %d\n\n",(int)[[UIScreen mainScreen] nativeBounds].size.width,(int)[[UIScreen mainScreen] nativeBounds].size.height);
    
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
        {
            if([[UIScreen mainScreen] nativeBounds].size.height == 960 || [[UIScreen mainScreen] nativeBounds].size.height == 480){
                printf("Device Type : iPhone 4,4s ");
    
            }else if([[UIScreen mainScreen] nativeBounds].size.height == 1136){
                printf("Device Type : iPhone 5,5S/iPod 5 ");
    
            }else if([[UIScreen mainScreen] nativeBounds].size.height == 1334){
                printf("Device Type : iPhone 6 ");
    
            }else if([[UIScreen mainScreen] nativeBounds].size.height == 2208){
                printf("Device Type : iPhone 6+ ");
    
            }
        }
    }else{
        printf("Device Type : iPad");
    }
    

    If the project was created in Xcode 5 and opened in Xcode 6, then use the below mentioned code to detect the devices.(This code works if no launching images for iPhone 6,6+ are assigned)

    printf("\nDetected Resolution : %d x %d\n\n",(int)[[UIScreen mainScreen] nativeBounds].size.width,(int)[[UIScreen mainScreen] nativeBounds].size.height);
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
        {
           if([[UIScreen mainScreen] nativeBounds].size.height == 960 || [[UIScreen mainScreen] nativeBounds].size.height == 480){
                printf("Device Type : iPhone 4,4s");
                appType=1;
            }else if([[UIScreen mainScreen] nativeBounds].size.height == 1136 || [[UIScreen mainScreen] nativeBounds].size.height == 1704){
                printf("Device Type : iPhone 5,5S,6,6S/iPod 5 ");
                appType=3;
            }
        }
    }else{
        printf("Device Type : iPad");
        appType=2;
    }
    

    If you are still using Xcode 5 all together then use the following code to detect the devices (iPhone 6 and 6+ will not be detected)

    printf("\nDetected Resolution : %d x %d\n\n",(int)[[UIScreen mainScreen] bounds].size.width,(int)[[UIScreen mainScreen] bounds].size.height);
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
        {
            CGSize result = [[UIScreen mainScreen] bounds].size;
            CGFloat scale = [UIScreen mainScreen].scale;
            result = CGSizeMake(result.width * scale, result.height * scale);
            if(result.height == 960 || result.height == 480){
                printf("Device Type : iPhone 4,4S ");
    
            }else if(result.height == 1136){
                printf("Device Type : iPhone 5s/iPod 5");
    
            }
        }
    }else{
        printf("Device Type : iPad");
    
    }
    
    0 讨论(0)
  • 2020-11-22 01:09
    1. Add a 'New Swift File'-> AppDelegateEx.swift

    2. add an extension to AppDelegate

      import UIKit
      extension AppDelegate {
           class func isIPhone5 () -> Bool{
               return max(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) == 568.0
          }
          class func isIPhone6 () -> Bool {
              return max(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) == 667.0
          }
          class func isIPhone6Plus () -> Bool {
              return max(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) == 736.0
          }  
      }
      
    3. usage:

          if AppDelegate.isIPhone5() {
              collectionViewTopConstraint.constant = 2
          }else if AppDelegate.isIPhone6() {
              collectionViewTopConstraint.constant = 20
          }
      
    0 讨论(0)
提交回复
热议问题