1. 程式人生 > >iOS 裝置獲取判斷

iOS 裝置獲取判斷

/**
 *  裝置型別
 */
enum DeviceType: Int {
    case Iphone4          // iPhone 4S
    case Iphone5          // iPhone SE
    case Iphone6         // iPhone 6
    case Iphone6P        // iPhone 6 Plus
    case IphoneX        // iPhone X   iPhone XS
    case IphoneMax     //iPhone Xs Max  iPhone XR
    
    var height: Int {
        switch self {
            case .Iphone4:        return 480
            case .Iphone5:        return 568
            case .Iphone6:        return 667
            case .Iphone6P:       return 736
            case .IphoneX:        return 812
            case .IphoneMax:      return 896
        }
    }
}
extension UIDevice {
    
   static func currentDeviceType() -> DeviceType{
        switch screenHeight {
        case 480:
            return .Iphone4
        case 568:
            return .Iphone5
        case 667:
            return .Iphone6
        case 736:
            return .Iphone6P
        case 812:
            return .IphoneX
        case 896:
            return .IphoneMax
        default:
            return .Iphone6
        }
    }
    
}