Take the 2-minute tour ×

How can one get the dimensions of the screen in iOS?

Currently, I use:

lCurrentWidth = self.view.frame.size.width;
lCurrentHeight = self.view.frame.size.height;

in viewWillAppear: and willAnimateRotationToInterfaceOrientation:duration:

The first time I get the entire screen size. The second time i get the screen minus the nav bar.

share|improve this question
add comment

7 Answers

up vote 466 down vote accepted

The problem with the code that you posted is that you're counting on the view size to match that of the screen, and as you've seen that's not always the case. If you need the screen size, you should look at the object that represents the screen itself, like this:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
share|improve this answer
Orientation is really managed at the view controller level. Take a look at Managing a View Controller's Interface Orientation. So yes, look at your view controller's interfaceOrientation property. BTW, you asked about the screen size, so that's what I showed you, but for displaying your content you should probably use the window's bounds or the screen's applicationFrame, depending on what you're doing. –  Caleb Apr 15 '11 at 15:25
Thank you. I couldn't work those methods out before, but i will try again. –  griotspeak Apr 15 '11 at 15:31
it's useful to note that this returns the full screen width without padding. Simply subtract your app's padding (usually 20 on each side) from these results to get the 'usable' space for most elements –  Nick Daugherty Aug 30 '12 at 16:25
@NickDaugherty Yes, that's the point -- the OP wanted the screen dimensions. Where you put objects on the screen is entirely up to you and depends on what type of app you're building. If you're displaying a photo or a game interface, for example, you might not want any padding at all. –  Caleb Aug 30 '12 at 16:29
Note however that this returns a value measured in points. So if you were expecting the screen resolution in pixels, the result would be incorrect, and you should multiply the result by [UIScreen scale]. –  Robotbugs Aug 20 '13 at 1:48
show 4 more comments

I have used these convenience methods before:

- (CGRect)getScreenFrameForCurrentOrientation {
    return [self getScreenFrameForOrientation:[UIApplication sharedApplication].statusBarOrientation];

- (CGRect)getScreenFrameForOrientation:(UIInterfaceOrientation)orientation {

    UIScreen *screen = [UIScreen mainScreen];
    CGRect fullScreenRect = screen.bounds;
    BOOL statusBarHidden = [UIApplication sharedApplication].statusBarHidden;

    //implicitly in Portrait orientation.
    if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft){
      CGRect temp = CGRectZero;
      temp.size.width = fullScreenRect.size.height;
      temp.size.height = fullScreenRect.size.width;
      fullScreenRect = temp;

      CGFloat statusBarHeight = 20;//Needs a better solution, FYI statusBarFrame reports wrong in some cases..
      fullScreenRect.size.height -= statusBarHeight;

    return fullScreenRect;
share|improve this answer
Nice solution, but be careful that temp seems to have undefined origin. –  szemian Jul 22 '12 at 13:17
So your saying inferring 0,0, origin isn't acceptable? –  Luke Mcneice Jul 22 '12 at 14:26
Do you get 0,0 as default? When I logged the fullScreenRect, it gave me: {{8.03294e-35, 3.09816e-40}, {480, 320}}. I chose to initialized with CGRect temp = CGRectZero; –  szemian Jul 22 '12 at 14:40
UIInterfaceOrientationIsLandscape does not handle 'UIDeviceOrientationFaceUp' and 'UIDeviceOrientationFaceDown' which may be returned from UIDevice. –  Andriy Aug 13 '12 at 14:53
You may like to use the property screen.applicationFrame instead of screen.bounds which takes care of the status bar. –  Geraud.ch Oct 9 '12 at 11:50
show 4 more comments

Careful, [UIScreen mainScreen] contains status bar as well, if you want to retrieve the frame for your application (excluding status bar) you should use

+ (CGFloat) window_height   {
    return [UIScreen mainScreen].applicationFrame.size.height;

+ (CGFloat) window_width   {
    return [UIScreen mainScreen].applicationFrame.size.width;
share|improve this answer
add comment

It's very, very easy to get your device size as well as take into account the orientation:

// grab the window frame and adjust it for orientation
UIView *rootView = [[[UIApplication sharedApplication] keyWindow] 
CGRect originalFrame = [[UIScreen mainScreen] bounds];
CGRect adjustedFrame = [rootView convertRect:originalFrame fromView:nil];
share|improve this answer
Hi, I use this in viewDidLoad on an iPad landscape only app, is there any reason why it gets the correct value first time, then the value 'flips' on every load after than, ie it reads it as lanscape first view, then I navigate away and back to the view and it seems to read it as portrait even tho no orientation change was made? Thanks –  craigk Apr 11 '13 at 4:14
Doesn't work for me either - adjustedFrame is CGrectZero –  Luke Mcneice Apr 18 '13 at 13:58
@LukeMcneice In my production code I have an assert to make sure that a frame is found...I have yet to have that assert fail. What is being returned for rootView and originalFrame? –  Michael G. Emmons Apr 18 '13 at 15:07
I am using this technique in a singleton to get the orientation-corrected width/height. However, if this code runs while a UIAlert window showing the convertRect returns adjustedFrame with a size (width at least) of 0. Any thoughts what is going on with that? –  Dawgless Boyd Apr 19 '13 at 13:41
Ah. An UIAlertView will insert itself as the keyWindow's rootViewController view. I have logic that avoids the corner case because if an alert view is up, the user can't interact with the keyboard, so there is typically no need to grab the keyboard frame at that time. –  Michael G. Emmons Apr 19 '13 at 16:14
show 1 more comment

We have to consider the orientation of device too:

CGFloat screenHeight;
// it is important to do this after presentModalViewController:animated:
if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortraitUpsideDown){
    screenHeight = [UIScreen mainScreen].applicationFrame.size.height;
    screenHeight = [UIScreen mainScreen].applicationFrame.size.width;
share|improve this answer
add comment

I realize that this is an old post, but sometimes I find it useful to #define constants like these so I do not have to worry about it:

#define DEVICE_SIZE [[[[UIApplication sharedApplication] keyWindow] rootViewController].view convertRect:[[UIScreen mainScreen] bounds] fromView:nil].size

The above constant should return the correct size no matter the device orientation. Then getting the dimensions is as simple as:

lCurrentWidth = DEVICE_SIZE.width;
lCurrentHeight = DEVICE_SIZE.height;
share|improve this answer
You might want to base the macro on AnswerBot's answer since that seems like the best answer. –  griotspeak Aug 8 '13 at 13:17
Thanks for the suggestion. I changed the macro. –  A Random User Aug 9 '13 at 14:14
add comment
NSLog(@"%.0f", [[UIScreen mainScreen] bounds].size.width);
NSLog(@"%.0f", [[UIScreen mainScreen] bounds].size.height);
share|improve this answer
thank you dude,its working –  Mubin Shaikh May 17 at 11:36
add comment

Your Answer


