How to get device width and height?

后端 未结 12 1119
离开以前
离开以前 2020-12-23 14:13

In Objective-C we can get device width and height by using following code :

CGRect sizeRect = [UIScreen mainScreen].applicationFrame
float width = sizeRect.         


        
12条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-23 14:58

    If you want to use it in your code. Here you go.

    func iPhoneScreenSizes() {
        let bounds = UIScreen.main.bounds
        let height = bounds.size.height
    
        switch height {
        case 480.0:
            print("iPhone 3,4")
        case 568.0:
            print("iPhone 5")
        case 667.0:
            print("iPhone 6")
        case 736.0:
            print("iPhone 6+")
        case 812.0:
            print("iPhone X")
            print("iPhone XS")
            break
        case 896.0:
            print("iPhone XR")
            print("iPhone XS Max")
            break
        default:
            print("not an iPhone")
    
        }
    }
    

提交回复
热议问题