如何获得iPhone屏幕尺寸来计算?
您可以在UIScreen的实例上使用bounds
属性:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
大多数人都想要主屏幕;如果你有一台设备连接到电视,你可能想要迭代UIScreens并获得每个设备的界限。
更多信息在UIScreen class docs。
假设您想考虑当前的方向,请使用:
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;
}
对于同伴Xamarians - 这是你可以在Xamarin.iOS + C#中获得屏幕尺寸的方式:
var screenBound = UIScreen.MainScreen.Bounds;
var screenSize = screenBound.Size;
var height = screenSize.Height;
var width = screenSize.Width;
这是一个'迅捷'的解决方案:(更新为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)
请注意,对于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;
}
如果您想知道某个视图或bounds
的大小,如果您想知道设备的屏幕大小,请使用UIView
的属性[[UIScreen mainScreen] bounds]
。
与上述相似但略显冗长:
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
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);
}
这是获取屏幕尺寸的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
}
这些包括作为标准函数here。
这也在文档中。
[[UIScreen mainScreen] bounds]返回物理屏幕尺寸,而不考虑设备方向。
如果您需要根据当前方向获取屏幕尺寸,请查看How to get orientation-dependent height and width of the screen?
使用UIScreen
的边界是一个很好的方法,但你应该使用CGGeometry
函数来访问CGRect
的数据,而不是直接访问它。参见CGGeometry
docs。
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = CGRectGetWidth(screenBound);
CGFloat screenHeight = CGRectGetHeight(screenBound);