Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get orientation-dependent height and width of the screen?

Tags:

ios

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?

like image 759
MusiGenesis Avatar asked Oct 26 '11 16:10

MusiGenesis


People also ask

How can we detect the orientation of the screen?

Detecting screen orientation is easy - and in the future we'll be able to use screen. orientation to reliably do this. For now, it's best to stick with CSS media queries and window. matchMedia .

Which orientation style has more height than width?

The term "portrait orientation" comes from visual art terminology and describes the dimensions used to capture a person's face and upper body in a picture; in such images, the height of the display area is greater than the width.

How do I change the orientation of my screen on Android?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.


2 Answers

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.

Hope this helps. :D

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!

like image 195
Sam Avatar answered Sep 26 '22 01:09

Sam


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 ; } 
like image 34
monjer Avatar answered Sep 24 '22 01:09

monjer