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

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

haisergeant asked 2020-02-23T00:00:16Z
11个解决方案
100 votes

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

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

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

更多信息,请参见UIScreen类文档。

Tim answered 2020-02-23T00:00:32Z
22 votes

这是一个“快速”解决方案:(已更新为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

这也“基于设备朝上放置。设备旋转时该值不会改变。”(Apple Docs)

请注意,对于2.x和更低版本,请使用UIScreen.mainScreen()而不是UIScreen.main

mogelbuster answered 2020-02-23T00:01:20Z
8 votes

使用此代码在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;
}
Guru answered 2020-02-23T00:01:39Z
1 votes

如果要了解某个视图的大小,请使用UIView的属性bounds,如果要了解设备的屏幕大小,请使用[[UIScreen mainScreen] bounds]

tobiasbayer answered 2020-02-23T00:02:00Z
1 votes

与上述类似,但略有冗长:

CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
simon_smiley answered 2020-02-23T00:02:19Z
1 votes
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);
}
Bobby answered 2020-02-23T00:02:35Z
1 votes

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

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
}

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

这也在文档中。

Esqarrouth answered 2020-02-23T00:03:04Z
0 votes

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

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

Mike answered 2020-02-23T00:03:28Z
0 votes

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

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = CGRectGetWidth(screenBound);
CGFloat screenHeight = CGRectGetHeight(screenBound);
Lukas Spieß answered 2020-02-23T00:03:49Z
0 votes

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

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;
}
CpnCrunch answered 2020-02-23T00:04:09Z
0 votes

对于Xamarian同伴-这是在Xamarin.iOS + C#中获取屏幕尺寸的方法:

var screenBound = UIScreen.MainScreen.Bounds;
var screenSize = screenBound.Size;
var height = screenSize.Height;
var width = screenSize.Width;
Martin Zikmund answered 2020-02-23T00:04:29Z
translate from https://stackoverflow.com:/questions/3635483/how-to-get-screen-size-using-code-on-ios