97

I'm trying to programmatically determine the current height and width of my application. I use this:

CGRect screenRect = [[UIScreen mainScreen] bounds];

But this yields a width of 320 and a height of 480, regardless of whether the device is in portrait or landscape orientation. How can I determine the current width and height (i.e. dependent upon the device orientation) of my main screen?

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334

12 Answers12

164

You can use something like UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) to determine the orientation and then use the dimensions accordingly.

HOWEVER, during an orientation change like in UIViewController's

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                 duration:(NSTimeInterval)duration

Use the orientation passed in toInterfaceOrientation since the UIApplication's statusBarOrientation will still point to the old orientation as it has not yet changed (since you're inside a will event handler).

Summary

There are several related posts to this, but each of them seem to indicate that you have to:

  1. Look at [[UIScreen mainScreen] bounds] to get the dimensions,
  2. Check what orientation you are in, and
  3. Account for the status bar height (if shown)

Links

Working Code

I usually don't go this far, but you piqued my interest. The following code should do the trick. I wrote a Category on UIApplication. I added class methods for getting the currentSize or the size in a given orientation, which is what you would call in UIViewController's willRotateToInterfaceOrientation:duration:.

@interface UIApplication (AppDimensions)
+(CGSize) currentSize;
+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation;
@end

@implementation UIApplication (AppDimensions)

+(CGSize) currentSize
{
    return [UIApplication sizeInOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation
{
    CGSize size = [UIScreen mainScreen].bounds.size;
    UIApplication *application = [UIApplication sharedApplication];
    if (UIInterfaceOrientationIsLandscape(orientation))
    {
        size = CGSizeMake(size.height, size.width);
    }
    if (application.statusBarHidden == NO)
    {
        size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height);
    }
    return size;
}

@end

To use the code simple call [UIApplication currentSize]. Also, I ran the above code, so I know it works and reports back the correct responses in all orientations. Note that I factor in the status bar. Interestingly I had to subtract the MIN of the status bar's height and width.

Other thoughts

You could go about getting the dimensions by looking at the UIWindow's rootViewController property. I've looked at this in the past and it similarly reports the same dimensions in both portrait and landscape except it reports having a rotate transform:

(gdb) po [[[[UIApplication sharedApplication] keyWindow] rootViewController] view]

<UILayoutContainerView: 0xf7296f0; frame = (0 0; 320 480); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0xf729b80>>

(gdb) po [[[[UIApplication sharedApplication] keyWindow] rootViewController] view]

<UILayoutContainerView: 0xf7296f0; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0xf729b80>>

Not sure how your app works, but if you aren't using a navigation controller of some kind, you could have a UIView under your main view with the max height / width of parent and grows / shrinks with parent. Then you could do: [[[[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] subviews] objectAtIndex:0] frame]. That looks pretty intense on one line, but you get the idea.

However, it would still be better to do the above 3 steps under the summary. Start messing with UIWindows and you'll find out weird stuff, like showing a UIAlertView will change UIApplication's keywindow to point at a new UIWindow that the UIAlertView created. Who knew? I did after finding a bug relying on keyWindow and discovering that it changed like that!

starball
  • 20,030
  • 7
  • 43
  • 238
Sam
  • 26,946
  • 12
  • 75
  • 101
  • 5
    This seems like such an elementary task that I'm having trouble believing I have to hack my own code to determine something like this. – MusiGenesis Oct 26 '11 at 16:11
  • 2
    I'll try another solution and repost if it works. This site is so aggressive if you don't answer quickly you might as well not answer at all. haha... :D I hope my answer is at least helpful. Again, I'll try something else out real quick. – Sam Oct 26 '11 at 16:16
  • thanks, I appreciate it. I've noticed that iPhone questions related to non-standard ways of doing things usually go unanswered, so there's probably no hurry. – MusiGenesis Oct 26 '11 at 16:32
  • Let me know what you think of the category I added. Should do the trick for you. – Sam Oct 26 '11 at 17:07
  • +1: great answer. I wish there were more like this. The Category on UIApplication works fine with rotation. – lucasart Oct 31 '11 at 07:09
  • See accepted answer update to [IPhone/IPad: How to get screen width programmatically?](http://stackoverflow.com/q/3655104/590956) for what I think is a simpler solution. – rimsky Jan 30 '12 at 19:48
  • I love you. I was having problems with this as well. Checking for the rootViewController's view bounds did the trick. – Andy Ibanez Oct 02 '12 at 21:31
  • hi guys the above code doesnt work for me in iOS6. it reported exactly the reverse. using iphone4S, when I hold my app portrait it reported a size of 480x320 and when I hold it in landscape it reported a size of 320x480. I can swap the width height myself but looks very wrong to do this. any advise? btw i m checking the width height after receiving a notification from UIDeviceOrientationDidChangeNotification... – mkto Nov 17 '12 at 17:03
  • 1
    If you use `applicationFrame` instead of `bounds` (on UIScreen), you don't have to subtract the status bar height. – aopsfan Mar 29 '13 at 13:22
  • I know this is old, but just so we are aware, the "MIN" function is no longer necessary in iOS7... there should be logic around it to check to see if the user is actually diplaying data UNDER the status bar or not. I dont, so i simply did "0 && " in front of "application.statusBarHidden == NO" for now...and will figure out the logic later :) – Jann Oct 16 '13 at 16:58
  • 1
    I got wrong size in portrait mode. The iOS is `9.0.2`. In the landscape and portrait mode, I got the same size `(568.0, 300.0)` on the iPhone 5s. – AechoLiu Oct 14 '15 at 06:04
  • 2
    http://stackoverflow.com/questions/24150359/is-uiscreen-mainscreen-bounds-size-becoming-orientation-dependent-in-ios8 `mainScreen().bounds.size` has become orientation dependent from iOS 8 onwards – Gerald Apr 10 '16 at 03:17
39

This is my solution code !This method can add to NSObject class's Categroy , or you can define a Top custom UIViewController class , and let all of your other UIViewControllers to inherit it .

-(CGRect)currentScreenBoundsDependOnOrientation
{  

    CGRect screenBounds = [UIScreen mainScreen].bounds ;
    CGFloat width = CGRectGetWidth(screenBounds)  ;
    CGFloat height = CGRectGetHeight(screenBounds) ;
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

    if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
        screenBounds.size = CGSizeMake(width, height);
    }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
        screenBounds.size = CGSizeMake(height, width);
    }
    return screenBounds ;
}

Note, after IOS8 , as Apple Document of UIScreen's bounds property says :

Discussion

This rectangle is specified in the current coordinate space, which takes into account any interface rotations in effect for the device. Therefore, the value of this property may change when the device rotates between portrait and landscape orientations.

so for the consideration of compatibility , we should detect the IOS version and make the change as below:

#define IsIOS8 (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)

-(CGRect)currentScreenBoundsDependOnOrientation
{  

    CGRect screenBounds = [UIScreen mainScreen].bounds ;
    if(IsIOS8){
        return screenBounds ;
    }
    CGFloat width = CGRectGetWidth(screenBounds)  ;
    CGFloat height = CGRectGetHeight(screenBounds) ;
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

    if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
        screenBounds.size = CGSizeMake(width, height);
    }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
        screenBounds.size = CGSizeMake(height, width);
    }
    return screenBounds ;
}
monjer
  • 2,809
  • 2
  • 21
  • 31
  • Pretty much the same as my answer, only thing it misses is if it is in Portrait upside down. This only matters if you support that orientation tho. – Robert Wagstaff Feb 12 '13 at 23:50
  • 2
    @Monjer you should not name methods that do not perform a GET request, prefixed i with the word get. currentScreenBoundsDependOnOrientation is a better name for the method – bogen May 30 '13 at 11:54
  • 1
    @Hakonbogen.yes may be you are right , because “peroperty” declaration automaticly generate setter/getter method , and this may lead to naming conflict , and go against objc's naming conventions.Thanks for your advice. – monjer May 31 '13 at 02:27
  • It's nice that Apple has *finally* tacitly acknowledged that their rotation code was a complete mess, and just started telling us what the damn dimensions are in the current orientation. Too bad it took until version 8 to get there. – MusiGenesis Sep 26 '14 at 17:46
31

Here's a handy macro:

#define SCREEN_WIDTH (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)
#define SCREEN_HEIGHT (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)
Ryan
  • 5,416
  • 1
  • 39
  • 36
Robert Wagstaff
  • 2,664
  • 1
  • 27
  • 40
14

In iOS 8+ you should use the viewWillTransitionToSize:withTransitionCoordinator method:

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    // You can store size in an instance variable for later
    currentSize = size;

    // This is basically an animation block
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        // Get the new orientation if you want
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

        // Adjust your views
        [self.myView setFrame:CGRectMake(0, 0, size.width, size.height)];

    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        // Anything else you need to do at the end
    }];
}

This replaces the deprecated animation method that gave no information about size:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
Mark H
  • 741
  • 8
  • 12
10

As of iOS 8 screen bounds are now returned correct for current orientation. This means an iPad in landscape orientation [UIScreen mainScreen].bounds would return 768 on iOS <=7 and 1024 on iOS 8.

The following returns the correct height and width on all versions released.

-(CGRect)currentScreenBoundsDependOnOrientation
{
    NSString *reqSysVer = @"8.0";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
        return [UIScreen mainScreen].bounds;

    CGRect screenBounds = [UIScreen mainScreen].bounds ;
    CGFloat width = CGRectGetWidth(screenBounds)  ;
    CGFloat height = CGRectGetHeight(screenBounds) ;
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

    if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
        screenBounds.size = CGSizeMake(width, height);
        NSLog(@"Portrait Height: %f", screenBounds.size.height);
    }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
        screenBounds.size = CGSizeMake(height, width);
        NSLog(@"Landscape Height: %f", screenBounds.size.height);
    }

    return screenBounds ;
}
A.Badger
  • 4,279
  • 1
  • 26
  • 18
8

if you want the orientation dependent size and you have a view, you can just use:

view.bounds.size
Tod
  • 4,618
  • 4
  • 32
  • 23
5

I wrote category for UIScreen, that works on all iOS versions, so you can use it like this:
[[UIScreen mainScreen] currentScreenSize].

@implementation UIScreen (ScreenSize)

- (CGSize)currentScreenSize {
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBounds.size;

    if ( NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1 ) {  
        UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        if ( UIInterfaceOrientationIsLandscape(interfaceOrientation) ) {
            screenSize = CGSizeMake(screenSize.height, screenSize.width);
        }
    }

    return screenSize;
}

@end
ArtFeel
  • 11,701
  • 4
  • 29
  • 41
5

Here is a Swift way to get orientation dependent screen sizes:

var screenWidth: CGFloat {
    if UIInterfaceOrientationIsPortrait(screenOrientation) {
        return UIScreen.mainScreen().bounds.size.width
    } else {
        return UIScreen.mainScreen().bounds.size.height
    }
}
var screenHeight: CGFloat {
    if UIInterfaceOrientationIsPortrait(screenOrientation) {
        return UIScreen.mainScreen().bounds.size.height
    } else {
        return UIScreen.mainScreen().bounds.size.width
    }
}
var screenOrientation: UIInterfaceOrientation {
    return UIApplication.sharedApplication().statusBarOrientation
}

These are included as a standard function in a project of mine:

https://github.com/goktugyil/EZSwiftExtensions

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
0
float msWidth = [[UIScreen mainScreen] bounds].size.width*(IS_RETINA?2.0f:1.0f);
float msHeight = [[UIScreen mainScreen] bounds].size.height*(IS_RETINA?2.0f:1.0f);
if ( UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ) {
    os->setWidth(MIN(msWidth, msHeight));
    os->setHeight(MAX(msWidth, msHeight));
} else {
    os->setWidth(MAX(msWidth, msHeight));
    os->setHeight(MIN(msWidth, msHeight));
}

NSLog(@"screen_w %f", os->getWidth());
NSLog(@"screen_h %f", os->getHeight());
0

However, on iOS 8.0.2:

+ (NSUInteger)currentWindowWidth
{
    NSInteger width = 0;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    CGSize size = [UIScreen mainScreen].bounds.size;
   // if (UIInterfaceOrientationIsLandscape(orientation)) {
   //     width = size.height;
   // } else {
        width = size.width;
  //  }

    return width;
}
Gank
  • 4,507
  • 4
  • 49
  • 45
0

use -> setNeedsDisplay() for the view you want to resize.

Santosh
  • 1,275
  • 1
  • 10
  • 21
0

Some improvements on the answers offered here, in Swift:

    let interfaceOrientation = UIApplication.shared.statusBarOrientation // (< iOS 13)
    let screenSize = UIScreen.main.bounds.size
    let screenWidth = interfaceOrientation.isPortrait ? screenSize.width : screenSize.height
    let screenHeight = interfaceOrientation.isPortrait ? screenSize.height : screenSize.width
Kqtr
  • 5,824
  • 3
  • 25
  • 32