Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6719333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:04:49+00:00 2026-05-26T09:04:49+00:00

I’m trying to programmatically determine the current height and width of my application. I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:04:50+00:00Added an answer on May 26, 2026 at 9:04 am

    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

    • Iphone: Get current view dimensions or screen dimensions
    • IPhone/IPad: How to get screen width programmatically?
    • Objective C – how to get current screen resolution?
    • “Incorrect” frame / window size after re-orientation in iPhone or iPad
    • iPhone Dev SDK – get screen width

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.