2013-01-03 163 views
19

可能重复:
IPhone/IPad: How to get screen width programmatically?
How to get orientation-dependent height and width of the screen?如何获得iOS设备屏幕的高度和宽度

占各种iOS设备和方向,什么是最简单的方法来获得当前的屏幕分辨率,包括高度和宽度?

+1

也或者[如何获得定向相关的高度和屏幕的宽度是多少?(http://stackoverflow.com/questions/7905432/how-to-get-orientation依赖性-高度和宽度的最屏幕/ 7905540#7905540)。 –

+0

在这里你有它:http://stackoverflow.com/questions/3655104/iphone-ipad-how-to-get-screen-width-programmatically –

+0

这些技术都没有为我工作。 – colindunn

回答

69

UIScreen是你的朋友在这里。

CGRect screenRect = [[UIScreen mainScreen] bounds]; 
CGFloat screenWidth = screenRect.size.width; 
CGFloat screenHeight = screenRect.size.height; 
+3

谢谢。我现在意识到,我没有考虑到在景观中,高度实际上变成了宽度。 – colindunn

+0

如果将以上内容与UIDevice结合使用,您将得到一个很好的ENUM,它可以准确告诉您设备的方向。 'UIDeviceOrientation deviceOrientation = [UIDevice currentDevice] .orientation;' – ericWasTaken

7
CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

这会给你的整个屏幕在分辨率的点,所以它最典型的为320x480像素的iPhone。尽管iPhone4拥有更大的屏幕尺寸,但iOS仍然支持320x480而不是640x960。这主要是因为较旧的应用程序中断。

希望它可以帮助你..

相关问题