-1

Is there a way in Objective-C to quickly get the width and height of the screen of the device I am in?

I tried doing:

CGRect deviceDimension = [[UIScreen mainScreen] bounds];

However in an iPad landscape mode it still gives me 768 as the width and 1024 as the height. Any other idea? Or do I have to just use a macro? Basically I am trying to find a way to replace the following macro:

#ifdef IPHONE

#define SCREEN_WIDTH_PORTRAIT 320
#define SCREEN_WIDTH_LANDSCAPE 480

#else

#define SCREEN_WIDTH_PORTRAIT 768
#define SCREEN_WIDTH_LANDSCAPE 1024

#endif
Costique
  • 23,644
  • 4
  • 74
  • 78
adit
  • 30,916
  • 67
  • 221
  • 365
  • See: http://stackoverflow.com/questions/3655104/iphone-ipad-how-to-get-screen-width-programmatically – Logan Serman Jan 18 '12 at 21:22
  • possible duplicate of [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) – rob mayoff Jan 18 '12 at 21:44

2 Answers2

2

Combine that with

 if (UIDeviceOrientationIsLandscape( [[UIDevice currentDevice] orientation] )) { }

to distinguish 768x1024 from 1024x768.

Rayfleck
  • 12,116
  • 7
  • 46
  • 74
0

See the comment from Richard J. Ross III below.

You could do it like this:

- (CGSize)screenSizeWithRotation
{
    return CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
}

simonbs
  • 7,714
  • 13
  • 67
  • 113