How to detect iPhone 5 (widescreen devices)?

前端 未结 24 1331
我寻月下人不归
我寻月下人不归 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 01:13

    Tested and designed for any combination of SDK and OS:

    Swift

    Added iPad types. iPad 2 and iPad mini are non-retina iPads. While iPad Mini 2 & above, iPad 3, 4, iPad Air, Air 2, Air 3, and iPad Pro 9.7 have same logical resolution of 1024. iPad Pro has maxLength of 1366. Reference

    import UIKit
    
    public enum DisplayType {
        case unknown
        case iphone4
        case iphone5
        case iphone6
        case iphone6plus
        case iPadNonRetina
        case iPad
        case iPadProBig
        static let iphone7 = iphone6
        static let iphone7plus = iphone6plus
    }
    
    public final class Display {
        class var width:CGFloat { return UIScreen.main.bounds.size.width }
        class var height:CGFloat { return UIScreen.main.bounds.size.height }
        class var maxLength:CGFloat { return max(width, height) }
        class var minLength:CGFloat { return min(width, height) }
        class var zoomed:Bool { return UIScreen.main.nativeScale >= UIScreen.main.scale }
        class var retina:Bool { return UIScreen.main.scale >= 2.0 }
        class var phone:Bool { return UIDevice.current.userInterfaceIdiom == .phone }
        class var pad:Bool { return UIDevice.current.userInterfaceIdiom == .pad }
        class var carplay:Bool { return UIDevice.current.userInterfaceIdiom == .carPlay }
        class var tv:Bool { return UIDevice.current.userInterfaceIdiom == .tv }
        class var typeIsLike:DisplayType {
            if phone && maxLength < 568 {
                return .iphone4
            }
            else if phone && maxLength == 568 {
                    return .iphone5
            }
            else if phone && maxLength == 667 {
                return .iphone6
            }
            else if phone && maxLength == 736 {
                return .iphone6plus
            }
            else if pad && !retina {
                return .iPadNonRetina
            }
            else if pad && retina && maxLength == 1024 {
                return .iPad
            }
            else if pad && maxLength == 1366 {
                return .iPadProBig
            }
            return .unknown
        }
    }
    

    See it in action https://gist.github.com/hfossli/bc93d924649de881ee2882457f14e346

    Note: If e.g. iPhone 6 is in zoomed mode the UI is a zoomed up version of iPhone 5. These functions is not determining device type, but display mode thus iPhone 5 is the desired result in this example.

    Objective-C

    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    #define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
    
    #define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
    #define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
    #define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
    #define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
    #define IS_ZOOMED (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
    
    #define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
    #define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
    #define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
    #define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
    

    Usage: http://pastie.org/9687735

    Note: If e.g. iPhone 6 is in zoomed mode the UI is a zoomed up version of iPhone 5. These functions is not determining device type, but display mode thus iPhone 5 is the desired result in this example.

提交回复
热议问题