如何在iOS上使用代码获取屏幕尺寸?

50

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

12个回答

106

你可以在 UIScreen 实例上使用 bounds 属性:

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

大多数人会想要主屏幕;如果您已经将设备连接到电视,则可能需要迭代UIScreens并获取每个屏幕的边界。

更多信息请参考UIScreen类文档


1
如果屏幕是视网膜显示屏,这段代码将无法正常工作 - 需要考虑到缩放值 - Matthew Herbst
1
这取决于你对尺寸的定义。返回值以点为单位,这通常是处理视图层次结构时所需的单位。如果您需要像素尺寸,则比例因子会发挥作用;然而,在我的经验中,这是一个较少见的需求。 - Tim
请记住设备方向的影响。如果您想确定您是否在特定设备上,请不要仅仅假设为纵向方向。 - Murray Sagal
如果您的应用程序仅设计为横向或纵向模式,则需要比较宽度和高度以确定正确的尺寸。我们无法再可靠地获取方向。例如,我有一个仅设计为纵向模式的应用程序,但如果在iPad上以横向模式打开,则宽度和高度会混淆。 - Bobby

26

这里有一个快速的解决方案: (更新至 Swift 3.x)

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

这个报告使用点而不是像素,并且“始终反映设备以纵向朝上方向的屏幕尺寸”(Apple文档)。无需关注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

这也“基于设备处于竖屏朝上的方向。随着设备旋转,此值不会改变。”(Apple 文档)

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


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;
}

在这个if语句中,heightwidth被赋值给了错误的变量。 - Murray Sagal
@MurraySagal 这就是重点,当iOS 7及以下版本处于横屏状态时,它会检查方向并设置高度和宽度不同。 - cortices

1

这里是获取屏幕尺寸的Swift方式:

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
}

这些作为标准功能 在这里 包括在内。

这也在文档中提到了。


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

是的,iOS 8“修复”了主屏幕大小,现在所有使用“hack”的旧应用程序都必须更新 :( - Coolant

1
与上面类似,但稍微简洁一些:

(保留HTML)

CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;

1
如果您想知道特定视图的大小,可以使用UIView的属性bounds,如果您想知道设备屏幕的大小,则可以使用[[UIScreen mainScreen] bounds]

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;
}

0

对于 Xamarin 开发者 - 这是在 Xamarin.iOS + C# 中获取屏幕尺寸的方法:

var screenBound = UIScreen.MainScreen.Bounds;
var screenSize = screenBound.Size;
var height = screenSize.Height;
var width = screenSize.Width;

0

使用UIScreen的边界是一个不错的方法,但你应该使用CGGeometry函数来访问CGRect的数据,而不是直接访问它。请参阅CGGeometrydocs

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = CGRectGetWidth(screenBound);
CGFloat screenHeight = CGRectGetHeight(screenBound);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接