回答
您可以使用UIScreen的一个实例bounds
属性:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
大多数人都希望在主屏幕;如果你有一台设备连接到电视机,你可能想要遍历UIScreens并获得每个设备的界限。
更多信息在UIScreen class docs。
如果您想知道某个视图的大小,或者想要知道设备的屏幕大小,请使用UIView
的属性bounds
或[[UIScreen mainScreen] bounds]
。
感谢您的回复 – haisergeant 2010-09-03 12:18:20
[[UIScreen mainScreen]界限]返回不考虑设备方向的物理屏幕尺寸。
如果您需要根据当前的方向得到的屏幕尺寸请看How to get orientation-dependent height and width of the screen?
相似但略有更简洁:
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
使用此代码在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'分配给错误的变量。 – 2015-03-12 01:37:45
@MurraySagal这就是重点,它检查方向,并在iOS 7和更低版本中设置高度和宽度,当它处于横向模式时。 – cortices 2015-06-30 07:02:23
使用UIScreen
的边界是一个很好的方法,但您应该使用CGGeometry
函数来访问CGRect
的数据,而不是直接访问它。请参阅CGGeometry
docs。
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = CGRectGetWidth(screenBound);
CGFloat screenHeight = CGRectGetHeight(screenBound);
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'固定'主屏幕大小,现在所有使用'黑客'的旧应用程序必须更新:( – Coolant 2014-09-22 05:43:50
假设你想利用当前的方向考虑,使用:
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;
}
这里是一个 '迅速' 解决方案: (更新了迅速3.X)
let screenWidth = UIScreen.main.fixedCoordinateSpace.bounds.width
let screenHeight = UIScreen.main.fixedCoordinateSpace.bounds.height
这个报告的点数不是像素,“总是反映设备在屏幕上的屏幕尺寸entation“(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
这里是一个斯威夫特的方式来获得屏幕尺寸:
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。
这也是在文档中。
- 1. 如何获取主屏幕Widget大小
- 2. 获取屏幕大小XCode
- 3. 如何使用ViewHolder获取屏幕大小?
- 4. 如何获取屏幕大小和作为资源使用 - WP8
- 5. 如何获得屏幕的大小?
- 6. 获取屏幕的实际大小
- 7. 如何获取控制台应用程序的屏幕大小?
- 8. android获取其他屏幕方向的屏幕大小
- 9. Android屏幕取向大小
- 10. 使用此代码检测iPhone屏幕大小可以吗?
- 11. 使用代码捕获屏幕
- 12. 如何使用jquery获得相对于屏幕大小的50%?
- 13. 使用xamarin获取android片段的屏幕大小
- 14. 使用PHP获取iframe或屏幕的大小
- 15. 获取屏幕的大小不使用getMetrics
- 16. 如何获取Jumbotron img以调整屏幕大小?
- 17. 如何在android中获取屏幕大小?
- 18. 如何在Windows Phone 7系列上获取屏幕大小?
- 19. 如何获取设备的屏幕大小?
- 20. 如何在Firemonkey FM3中获取屏幕大小?
- 21. 如何在AndroidStudio中获取设备屏幕大小?
- 22. 如何获取屏幕的总像素大小
- 23. 如何从JavaScript中获取Android WebView的屏幕大小
- 24. 如何获取屏幕大小并对其作出响应?
- 25. 如何在Xamarin.Forms中获取/检测屏幕大小?
- 26. 如何获取屏幕的物理大小?
- 27. Android:如何获取设备的真实屏幕大小?
- 28. 如何使用wxPython获取屏幕DPI?
- 29. 如何在Silverlight中获取屏幕工作区(不是整个屏幕大小)大小?
- 30. 如何在Android的画布中获取屏幕大小的大小?
这是工作,感谢您的回应。 – haisergeant 2010-09-03 12:18:04
如果屏幕是视网膜屏幕,此代码不起作用 - 需要考虑[比例值](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScreen_Class/Reference/ UIScreen.html#// apple_ref/OCC/instp/UIScreen /刻度)。 – 2014-04-27 21:01:42
这取决于你的尺寸的定义。返回的值以点为单位,在处理时通常是所需的单位,例如视图层次结构。如果你需要像素尺寸,是的,比例因子起作用;但根据我的经验,这是一种罕见的需求。 – Tim 2014-04-28 11:51:49