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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24084941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:36:00  来源:igfitidea点击:

How to get device width and height?

iosswiftcocoa-touchuiscreencgsize

提问by Rigel Networks

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 ?

如何用 Swift 做到这一点?

回答by iPatel

I haven't tried but it should be..

我没试过,但应该是..

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

回答by Adam Smaka

Swift 4.2

斯威夫特 4.2

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

回答by Pascal

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

@Houssni 的回答是正确的,但由于我们在谈论 Swift 并且这个用例会经常出现,因此可以考虑扩展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! :)

万岁!:)

回答by bpolat

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

    }
}

回答by bpolat

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

Exactly like this, tested it also.

就是这样,也测试了一下。

回答by Paul Bonneville

(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 fixedCoordinateSpacea try:

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

let screenSize = UIScreen.main.fixedCoordinateSpace.bounds

回答by jeff-ziligy

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

回答by Wyetro

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

回答by Aks

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 :

苹果文档网址:

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

回答by ohthepain

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
let deviceDescription = NSScreen.main?.deviceDescription          
let screenSize = deviceDescription![.size]
let screenHeight = (screenSize as! NSSize).height