How to get orientation-dependent height and wid...

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 awill 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'swillRotateToInterfaceOrientation:duration:.

How to get orientation-dependent height and wid..._第1张图片

size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height);

Other thoughts

You could go about getting the dimensions by looking at the UIWindow's rootViewControllerproperty. 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!

你可能感兴趣的:(height,width,screen,and,of,the)