49

如何获取 iPhone 屏幕尺寸来进行计算?

4

12 回答 12

104

您可以bounds在 UIScreen 的实例上使用该属性:

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;  
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;

大多数人会想要主屏幕。如果您有一个连接到电视的设备,您可能希望遍历 UIScreens 并获取每个设备的边界。

UIScreen 类文档中的更多信息。

于 2010-09-03T12:07:51.037 回答
26

这是一个“迅速”的解决方案:(为 swift 3.x 更新)

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

这以点而不是像素报告,并且“始终以纵向向上的方向反映设备的屏幕尺寸” (Apple Docs)。无需为 UIAnnoyinglyLongDeviceOrientation 烦恼!

如果您希望宽度和高度反映设备方向:

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

这里的宽度和高度将根据屏幕是纵向还是横向来翻转值。

以下是如何以像素而不是点为单位测量屏幕尺寸:

let screenWidthInPixels = UIScreen.main.nativeBounds.width
let screenHeightInPixels = UIScreen.main.nativeBounds.height

这也“基于纵向向上的设备。该值不会随着设备的旋转而改变。” (苹果文档)

请注意,对于 swift 2.x 及更低版本,您应该使用UIScreen.mainScreen()而不是UIScreen.main

于 2015-03-01T06:07:53.523 回答
8

使用此代码在 iOS8 中修复:

float SW;
float SH;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (( [[[UIDevice currentDevice] systemVersion] floatValue]<8)  && UIInterfaceOrientationIsLandscape(orientation))
{
    SW = [[UIScreen mainScreen] bounds].size.height;
    SH = [[UIScreen mainScreen] bounds].size.width;
}
else
{
    SW = [[UIScreen mainScreen] bounds].size.width;
    SH = [[UIScreen mainScreen] bounds].size.height;
}
于 2014-03-24T03:56:54.580 回答
1

与上面类似,但略显冗长:

CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
于 2014-03-24T03:53:33.693 回答
1

如果您想知道某个视图的大小或者您想知道设备的屏幕大小,请使用 属性boundsUIView[[UIScreen mainScreen] bounds]

于 2010-09-03T12:07:52.237 回答
1
float screen_Height;
float screen_Width;

if ([[[UIDevice currentDevice] systemVersion] floatValue]  >= 8) {
    screen_Height = [[UIScreen mainScreen] bounds].size.height;
    screen_Width = [[UIScreen mainScreen] bounds].size.width;
} else {
    screen_Height = ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width);
    screen_Width = ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height);
}
于 2014-09-17T21:36:37.570 回答
1

这是获取屏幕尺寸的快速方法:

var screenWidth: CGFloat {
    if UIInterfaceOrientationIsPortrait(screenOrientation) {
        return UIScreen.mainScreen().bounds.size.width
    } else {
        return UIScreen.mainScreen().bounds.size.height
    }
}
var screenHeight: CGFloat {
    if UIInterfaceOrientationIsPortrait(screenOrientation) {
        return UIScreen.mainScreen().bounds.size.height
    } else {
        return UIScreen.mainScreen().bounds.size.width
    }
}
var screenOrientation: UIInterfaceOrientation {
    return UIApplication.sharedApplication().statusBarOrientation
}

这些在此处作为标准功能包含在内。

这也在文档中。

于 2015-11-26T07:12:29.380 回答
0

[[UIScreen mainScreen] bounds] 返回物理屏幕尺寸,不考虑设备方向。

如果您需要根据当前方向获取屏幕尺寸,请查看如何获取与方向相关的屏幕高度和宽度?

于 2012-08-21T10:10:36.320 回答
0

假设您要考虑当前方向,请使用:

float screen_width;
float screen_height;
float vers = [[[UIDevice currentDevice] systemVersion] floatValue];
UIInterfaceOrientation orient = [UIApplication sharedApplication].statusBarOrientation;
if (vers < 8 && UIInterfaceOrientationIsLandscape(orient))
{
    screen_height = [[UIScreen mainScreen] bounds].size.width;
    screen_width = [[UIScreen mainScreen] bounds].size.height;
}
else
{
    screen_width = [[UIScreen mainScreen] bounds].size.width;
    screen_height = [[UIScreen mainScreen] bounds].size.height;
}
于 2014-12-02T19:10:52.580 回答
0

使用UIScreen's bounds 是一种很好的方法,但您应该使用CGGeometry函数来访问 aCGRect的数据,而不是直接访问它。请参阅CGGeometry 文档

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = CGRectGetWidth(screenBound);
CGFloat screenHeight = CGRectGetHeight(screenBound);
于 2014-07-19T21:49:55.667 回答
0
var PT: CGFloat = 0;

override func viewDidLoad() {

    super.viewDidLoad()
    setPoints();

}

func setPoints() {

    let screenBounds = UIScreen.main.bounds

    let screenScale = UIScreen.main.scale

    let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale)

    
    let nativeBounds = UIScreen.main.nativeBounds

    let nativeScale = UIScreen.main.nativeScale

    
    let pxScreenWidth:CGFloat = nativeBounds.width;

    let ptScreenWidth:CGFloat = screenBounds.width;

    PT = pxScreenWidth/ptScreenWidth/nativeScale;

}

如何使用:

let label: UILabel = UILabel(frame: CGRect(x: 15 * PT, y: 10 * PT, width: self.view.frame.width - 30 * PT, height: self.view.frame.height - 20 * PT))
于 2020-10-01T17:09:30.017 回答
0

对于 Xamarin 同胞 - 这是在 Xamarin.iOS + C# 中获取屏幕大小的方法:

var screenBound = UIScreen.MainScreen.Bounds;
var screenSize = screenBound.Size;
var height = screenSize.Height;
var width = screenSize.Width;
于 2019-03-26T10:55:41.070 回答