如何获取iPhone屏幕尺寸进行计算?
你可以在 UIScreen 实例上使用 bounds
属性:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
大多数人会想要主屏幕;如果您已经将设备连接到电视,则可能需要迭代UIScreens并获取每个屏幕的边界。
更多信息请参考UIScreen类文档。
这里有一个快速的解决方案: (更新至 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
。
使用以下代码来修复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;
}
height
和width
被赋值给了错误的变量。 - Murray Sagal这里是获取屏幕尺寸的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
}
这些作为标准功能 在这里 包括在内。
这也在文档中提到了。
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);
}
(保留HTML)
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
UIView
的属性bounds
,如果您想知道设备屏幕的大小,则可以使用[[UIScreen mainScreen] bounds]
。假设您想考虑当前方向,请使用:
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;
}
对于 Xamarin 开发者 - 这是在 Xamarin.iOS + C# 中获取屏幕尺寸的方法:
var screenBound = UIScreen.MainScreen.Bounds;
var screenSize = screenBound.Size;
var height = screenSize.Height;
var width = screenSize.Width;
使用UIScreen
的边界是一个不错的方法,但你应该使用CGGeometry
函数来访问CGRect
的数据,而不是直接访问它。请参阅CGGeometry
docs。
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = CGRectGetWidth(screenBound);
CGFloat screenHeight = CGRectGetHeight(screenBound);