86

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

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

如何在 Swift 中做到这一点?

4

13 回答 13

167

我没试过,但应该是..

var bounds = UIScreen.main.bounds
var width = bounds.size.width
var height = bounds.size.height
于 2014-06-06T15:07:37.723 回答
22

斯威夫特 4.2

let screenBounds = UIScreen.main.bounds
let width = screenBounds.width
let height = screenBounds.height
于 2016-06-21T07:52:28.550 回答
18

@Houssni 的回答是正确的,但是由于我们在谈论 Swift 并且这个用例会经常出现,因此可以考虑CGRect进行类似这样的扩展:

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

然后你可以像这样使用它:

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

万岁!:)

于 2014-06-06T15:45:00.153 回答
17

如果你想在你的代码中使用它。干得好。

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")

    }
}
于 2016-03-04T17:34:54.727 回答
10

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

let screenSize = UIScreen.main.fixedCoordinateSpace.bounds
于 2016-09-12T18:59:00.597 回答
8
var sizeRect = UIScreen.mainScreen().applicationFrame
var width    = sizeRect.size.width
var height   = sizeRect.size.height

正是这样,也测试了它。

于 2014-06-06T15:08:47.930 回答
5

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

let screenSize = UIScreen.mainScreen().bounds.size
let width = screenSize.width
let height = screenSize.height
于 2015-09-29T13:02:47.800 回答
3

虽然@Adam Smaka 的回答很接近,但在 Swift 3 中是这样的:

let screenBounds = UIScreen.main.bounds
let width = screenBounds.width
let height = screenBounds.height
于 2016-11-09T08:58:39.470 回答
3

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

苹果文档网址:

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

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

let screenHeight = UIScreen.main.bounds.height
let screenWidth = UIScreen.main.bounds.width
于 2017-03-14T21:19:47.487 回答
3

这适用于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")
        }
    }
于 2020-11-20T19:24:31.540 回答
2
 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
    }
于 2019-11-13T16:13:22.293 回答
2

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

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")
            }
        }
于 2021-01-20T14:51:33.703 回答
0

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

let deviceDescription = NSScreen.main?.deviceDescription          
let screenSize = deviceDescription![.size]
let screenHeight = (screenSize as! NSSize).height
于 2018-04-30T13:58:14.303 回答