2013-01-03 165 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。這主要是因爲較舊的應用程序中斷。

希望它可以幫助你..

相關問題