如何在 iOS 上使用代码获取屏幕大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3635483/
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-30 17:39:07  来源:igfitidea点击:

How to get screen size using code on iOS?

iosiphonecocoa-touchuiscreen

提问by haisergeant

How to get the iPhone screen size to give calculation?

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

回答by Tim

You can use the boundsproperty on an instance of UIScreen:

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

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

Most people will want the main screen; if you have a device attached to a TV, you may instead want to iterate over UIScreens and get the bounds for each.

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

More info at the UIScreen class docs.

UIScreen 类文档中的更多信息。

回答by mogelbuster

Here is a 'swift' solution: (Updated for swift 3.x)

这是一个“swift”解决方案:(针对 swift 3.x 更新)

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

This reports in points not pixels and "always reflect[s] the screen dimensions of the device in a portrait-up orientation"(Apple Docs). No need to bother with UIAnnoyinglyLongDeviceOrientation!

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

If you want the width and height to reflect the device orientation:

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

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

Here width and height will flip flop values depending on whether the screen is in portrait or landscape.

这里的宽度和高度将根据屏幕是纵向还是横向而触发值。

And here is how to get screen size measured in pixelsnot points:

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

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

This also "is based on the device in a portrait-up orientation. This value does not change as the device rotates."(Apple Docs)

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

Please note that for swift 2.x and lower, you should use UIScreen.mainScreen()instead of UIScreen.main

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

回答by Guru

Use This Code to fix in iOS8 :

使用此代码在 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;
}

回答by Bobby

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

回答by simon_smiley

Similar to above but slightly less verbose:

与上面类似,但稍微不那么冗长:

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

回答by tobiasbayer

Use the property boundsof UIViewif you want to know the size of a certain view or [[UIScreen mainScreen] bounds]if you want to know the screen size of the device.

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

回答by Esqarrouth

Here is a Swift way to get screen sizes:

这是获取屏幕尺寸的 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
}

These are included as a standard function here.

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

This is also in the documentation.

这也在文档中。

回答by Martin Zikmund

For fellow Xamarians - here is the way you can get the screen size in Xamarin.iOS + C#:

对于其他 Xamarians - 这是您可以在 Xamarin.iOS + C# 中获取屏幕大小的方法:

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

回答by Mike

[[UIScreen mainScreen] bounds] returns physical screen dimensions not taking device orientation into account.

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

If you need to get screen size according to the current orientation please look at How to get orientation-dependent height and width of the screen?

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

回答by CpnCrunch

Assuming you want to take the current orientation into account, use:

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

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