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

时间:2022-11-19 14:15:54

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

在Objective-C中,我们可以通过以下代码得到设备的宽度和高度:

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

How can do this in with Swift ?

斯威夫特怎么会这样?

10 个解决方案

#1


106  

I haven't tried but it should be..

我还没试过,但应该试一下。

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

#2


16  

@Houssni 's answer is correct, but since we're talking Swift and this use case will come up often, one could consider extending CGRect similar to this:

@Houssni的答案是正确的,但是由于我们讨论得很快,而且这个用例将会经常出现,我们可以考虑扩展CGRect,类似如下:

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

Then you can use it like:

然后你可以这样使用它:

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

Hooray! :)

万岁!:)

#3


11  

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

如果你想在代码中使用它。给你。

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


11  

Swift 4

斯威夫特4

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

#5


8  

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

Exactly like this, tested it also.

就像这样,测试了它。

#6


5  

(Swift 3) Keep in mind that most width and height values will be based on device's current orientation. If you want a consistent value that is not based on rotation and offers results as if you were in a portrait-up rotation, give fixedCoordinateSpace a try:

(Swift 3)记住,大多数宽度和高度值将基于设备当前的方向。如果你想要一个不基于旋转的一致的值,并提供结果,就像你在一个肖像旋转中一样,尝试一下fixedCoordinateSpace:

let screenSize = UIScreen.main.fixedCoordinateSpace.bounds

#7


4  

Since you're looking for the device screen size the simplest way is:

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

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

#8


2  

While @Adam Smaka's answer was close, in Swift 3 it is the following:

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

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

#9


1  

A UIScreen object defines the properties associated with a hardware-based display. iOS devices have a main screen and zero or more attached screens. Each screen object defines the bounds rectangle for the associated display and other interesting properties

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

Apple Doc URL :

苹果Doc网址:

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

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

To get Height/width of ur user's device with swift 3.0

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

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

#10


0  

In Swift 4 I had to use NSScreen.main?.deviceDescription

在Swift 4中,我必须使用NSScreen.main?.deviceDescription

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

#1


106  

I haven't tried but it should be..

我还没试过,但应该试一下。

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

#2


16  

@Houssni 's answer is correct, but since we're talking Swift and this use case will come up often, one could consider extending CGRect similar to this:

@Houssni的答案是正确的,但是由于我们讨论得很快,而且这个用例将会经常出现,我们可以考虑扩展CGRect,类似如下:

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

Then you can use it like:

然后你可以这样使用它:

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

Hooray! :)

万岁!:)

#3


11  

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

如果你想在代码中使用它。给你。

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


11  

Swift 4

斯威夫特4

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

#5


8  

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

Exactly like this, tested it also.

就像这样,测试了它。

#6


5  

(Swift 3) Keep in mind that most width and height values will be based on device's current orientation. If you want a consistent value that is not based on rotation and offers results as if you were in a portrait-up rotation, give fixedCoordinateSpace a try:

(Swift 3)记住,大多数宽度和高度值将基于设备当前的方向。如果你想要一个不基于旋转的一致的值,并提供结果,就像你在一个肖像旋转中一样,尝试一下fixedCoordinateSpace:

let screenSize = UIScreen.main.fixedCoordinateSpace.bounds

#7


4  

Since you're looking for the device screen size the simplest way is:

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

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

#8


2  

While @Adam Smaka's answer was close, in Swift 3 it is the following:

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

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

#9


1  

A UIScreen object defines the properties associated with a hardware-based display. iOS devices have a main screen and zero or more attached screens. Each screen object defines the bounds rectangle for the associated display and other interesting properties

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

Apple Doc URL :

苹果Doc网址:

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

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

To get Height/width of ur user's device with swift 3.0

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

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

#10


0  

In Swift 4 I had to use NSScreen.main?.deviceDescription

在Swift 4中,我必须使用NSScreen.main?.deviceDescription

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