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

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

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

斯威夫特怎么能这样?

95594 次浏览

I haven't tried but it should be..

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

Exactly like this, tested it also.

@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:

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! :)

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

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


}
}

Swift 4.2

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

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

let screenSize = UIScreen.main.fixedCoordinateSpace.bounds

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

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

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

Apple Doc URL :

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

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

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

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

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

This works great for 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")
}
}

Here's an updated list of the sizes within a useable function:

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

let bounds = UIScreen.main.bounds