如何获得设备的宽度和高度?

时间:2014-06-06 15:00:36

标签: ios swift cocoa-touch uiscreen cgsize

在Objective-C中,我们可以使用以下代码获取设备宽度和高度:

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

如何使用Swift执行此操作?

13 个答案:

答案 0 :(得分:119)

我没试过,但应该是......

var bounds = UIScreen.main.bounds
var width = bounds.size.width
var height = bounds.size.height

答案 1 :(得分:18)

@Houssni的答案是正确的,但由于我们正在谈论Swift,这个用例会经常出现,可以考虑将CGRect扩展为类似于此:

extension CGRect {
    var wh: (w: CGFloat, h: CGFloat) {
        return (size.width, size.height)
    }
}

然后你就可以使用它:

let (width, height) = UIScreen.mainScreen().applicationFrame.wh

万岁! :)

答案 2 :(得分:18)

Swift 4.2

let screenBounds = UIScreen.main.bounds
let width = screenBounds.width
let height = screenBounds.height

答案 3 :(得分:14)

如果要在代码中使用它。你走了。

func iPhoneScreenSizes(){
    let bounds = UIScreen.mainScreen().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+")

    default:
        print("not an iPhone")

    }


}

答案 4 :(得分:9)

var sizeRect = UIScreen.mainScreen().applicationFrame
var width    = sizeRect.size.width
var height   = sizeRect.size.height

正是这样,也进行了测试。

答案 5 :(得分:6)

(Swift 3)请记住,大多数宽度和高度值都将基于设备的当前方向。如果你想要一个不是基于旋转的一致值并且提供结果就像你在纵向旋转一样,那么试试 fixedCoordinateSpace

let screenSize = UIScreen.main.fixedCoordinateSpace.bounds

答案 6 :(得分:5)

由于您正在寻找设备屏幕尺寸,最简单的方法是:

let screenSize = UIScreen.mainScreen().bounds.size
let width = screenSize.width
let height = screenSize.height

答案 7 :(得分:3)

虽然@Adam Smaka的答案很接近,但在Swift 3中它是以下内容:

let screenBounds = UIScreen.main.bounds
let width = screenBounds.width
let height = screenBounds.height

答案 8 :(得分:2)

UIScreen对象定义与基于硬件的显示相关联的属性。 iOS设备具有主屏幕和零个或多个附加屏幕。每个屏幕对象定义关联显示的边界矩形和其他有趣的属性

Apple Doc网址:

  

https://developer.apple.com/reference/uikit/uiwindow/1621597-screen

使用swift 3.0获取用户设备的高度/宽度

let screenHeight = UIScreen.main.bounds.height
let screenWidth = UIScreen.main.bounds.width

答案 9 :(得分:1)

以下是可用函数中大小的更新列表:

func iScreenSizes() {
            let height = UIScreen.main.bounds.size.height
            print("Device height: \(height)")
            switch height {
            case 480.0:
                print("iPhone 3 | iPhone 4 | iPhone 4S")
            case 568.0:
                print("iPhone 5 | iPhone 5S | iPhone 5C | iPhone SE")
            case 667.0:
                print("iPhone 6 | iPhone 7 | iPhone 8 | iPhone SE(2nd gen)")
            case 736.0:
                print("iPhone 6+ | iPhone 7+ | iPhone 8+")
            case 780.0:
                print("iPhone 12 Mini")
            case 812.0:
                print("iPhone X | iPhone XS | iPhone 11 Pro")
            case 844.0:
                print("iPhone 12 | iPhone 12 Pro")
            case 896.0:
                print("iPhone XR | iPhone XS Max | iPhone 11 | iPhone 11 Pro Max")
            case 926.0:
                print("iPhone 12 Pro Max")
            case 1024.0:
                print("iPad 1st gen | iPad 2 | iPad 3rd gen | iPad mini | iPad 4th gen | iPad Air | iPad mini 2 | iPad mini 3 | iPad Air 2 | iPad mini 4 | iPad 5th gen | iPad 6th gen | iPad  mini 5")
            case 1112.0:
                print("iPad Pro 2nd gen 10.5'' | iPad Air 3")
            case 1194.0:
                print("iPad Pro 3rd gen 11.0'' | iPad Pro 4th gen 11.0''")
            case 1366.0:
                print("iPad Pro 1st gen 12.9'' | iPad 2nd gen 12.9'' | iPad 3rd gen 12.9'' | iPad Pro 4th gen 12.9''")
            default:
                print("not listed in function")
            }
        }

答案 10 :(得分:0)

在Swift 4中,我不得不使用NSScreen.main?.deviceDescription

let deviceDescription = NSScreen.main?.deviceDescription          
let screenSize = deviceDescription![.size]
let screenHeight = (screenSize as! NSSize).height

答案 11 :(得分:0)

 static func getDeviceType() -> String
    {
        var strDeviceType = ""
        if UIDevice().userInterfaceIdiom == .phone {
            switch UIScreen.main.nativeBounds.height {
            case 1136:
                strDeviceType = "iPhone 5 or 5S or 5C"
            case 1334:
                strDeviceType = "iPhone 6/6S/7/8"
            case 1920, 2208:
                strDeviceType = "iPhone 6+/6S+/7+/8+"
            case 2436:
                strDeviceType = "iPhone X"
            case 2688:
                strDeviceType = "iPhone Xs Max"
            case 1792:
                strDeviceType = "iPhone Xr"
            default:
                strDeviceType = "unknown"
            }
        }
        return strDeviceType
    }

答案 12 :(得分:0)

这非常适合 Xcode 12

func iPhoneScreenSizes() {
        let height = UIScreen.main.bounds.size.height
        switch height {
        case 480.0:
            print("iPhone 3,4")
        case 568.0:
            print("iPhone 5 | iPod touch(7th gen)")
        case 667.0:
            print("iPhone 6 | iPhone SE(2nd gen) | iPhone 8")
        case 736.0:
            print("iPhone 6+ | iPhone 8+")
        case 812.0:
            print("iPhone X | iPhone XS | iPhone 11 Pro")
        case 896.0:
            print("iPhone XR | iPhone XS Max | iPhone 11 | iPhone 11 Pro Max")
        default:
            print("not an iPhone")
        }
    }