23

Possible Duplicate:
IPhone/IPad: How to get screen width programmatically?
How to get orientation-dependent height and width of the screen?

Accounting for the various iOS devices and orientations, what is the simplest approach to getting the current screen resolution, including height and width?

Community
  • 1
  • 1
colindunn
  • 3,139
  • 10
  • 48
  • 72
  • 1
    Or also [How to get orientation-dependent height and width of the screen?](http://stackoverflow.com/questions/7905432/how-to-get-orientation-dependent-height-and-width-of-the-screen/7905540#7905540). – Kurt Revis Jan 03 '13 at 08:19
  • Here you have it: http://stackoverflow.com/questions/3655104/iphone-ipad-how-to-get-screen-width-programmatically – Amar Kulo Jan 03 '13 at 08:20
  • Neither of those techniques worked for me. – colindunn Jan 03 '13 at 08:21
  • 1
    What exactly didn't work? We can't help you if you don't tell us what your actual problems are. – Kurt Revis Jan 03 '13 at 08:27
  • @KurtRevis thanks for your feedback. I realize now that I wasn't accounting for the fact that in landscape, the height effectively becomes the width. – colindunn Jan 03 '13 at 08:28

2 Answers2

81

UIScreen is your friend here.

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • 3
    Thank you. I realize now that I wasn't accounting for the fact that in landscape, the height effectively becomes the width. – colindunn Jan 03 '13 at 08:24
  • If you combine the above with UIDevice, you get a nice ENUM that tells you exactly the orientation of the device. `UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;` – EricWasTaken Sep 25 '15 at 01:44
9
CGRect screenBounds = [[UIScreen mainScreen] bounds];

That will give you the entire screen's resolution in points, so it would most typically be 320x480 for iPhones. Even though the iPhone4 has a much larger screen size iOS still gives back 320x480 instead of 640x960. This is mostly because of older applications breaking.

Hope it helps you..

P.J
  • 6,547
  • 9
  • 44
  • 74