iOS How to detect iPhone X, iPhone 6 plus, iPhone 6, iPhone 5, iPhone 4 by macro?

前端 未结 13 620
一整个雨季
一整个雨季 2020-11-28 01:17

How to detect device model by macro? i had using something like this but the result on the simulator alway IS_IPHONE_5

#define IS_IPAD (UI_USER_INTERFACE_IDI         


        
相关标签:
13条回答
  • 2020-11-28 01:57
    public extension UIDevice {
    
        var iPhone: Bool {
            return UIDevice().userInterfaceIdiom == .Phone
        }
    
        enum ScreenType: String {
            case iPhone4
            case iPhone5
            case iPhone6
            case iPhone6Plus
            case Unknown
        }
        var screenType: ScreenType {
            guard iPhone else { return .Unknown}
            switch UIScreen.mainScreen().nativeBounds.height {
            case 960:
                return .iPhone4
            case 1136:
                return .iPhone5
            case 1334:
                return .iPhone6
            case 1920: //fallthrough
                return .iPhone6Plus
            case 2208:
                return .iPhone6Plus
            default:
                return .Unknown
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题