10

I have done what this question said here: Landscape Mode ONLY for iPhone or iPad

but the view.frame.size.height is still 1024, which is the height when the device is in portrait, surely when the interface rotates the width and height switch values?

(say you wanted to split the screen into 3 views, for an app that is both landscape and portrait, and you did view.frame.size.width / 3 , in landscape that wouldn't be correct as the width value wouldn't actually be the width)

I'm sure on the iPhone the width and height switch, so why not on the iPad?


This has struck me again I 'm not working with a nib either, could someone please give an acceptable answer? (ie one that doesn't involve manually switch the width and height)

Once the bounty has been awarded to an answer, I will then start another bounty for 250 and award it to the same person.

Community
  • 1
  • 1
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • Starting a bounty on this. The bounty will be awarded to the first person who posts code of a class that does this correctly. – William Jockusch Sep 10 '10 at 18:21
  • I'm not sure exactly what I did but I somehow "fixed" it. I got rid of my nib and rewrote the class – Jonathan. Sep 10 '10 at 18:56
  • I am working without a nib. But I can't get width and height to be correct after autorotation. – William Jockusch Sep 10 '10 at 20:09
  • Did you add your view controller to the view hierarchy? I experienced the same problem when I was just using the controller's view without proper child view controller containment. – leviathan Feb 25 '14 at 10:35

2 Answers2

24

You haven't specified which "view" you're querying. Assuming this is the top level subview of the window:

You should query the view's bounds not its frame. frame is in the coordinate in which the view is defined (the outside world) hence may remain constant as you rotate. bounds is the coordinate used "inside" the view and for its subviews. This does change when you rotate.

mohsenr
  • 7,235
  • 3
  • 28
  • 28
2
+ (int) currentWidth
{
 UIScreen *screen = [UIScreen mainScreen];
 int width = screen.currentMode.size.width;
 int height = screen.currentMode.size.height;
 return (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))? MAX (width, height) : MIN (width, height);
}

I spent a while trying to work out the simplest solution to a frustrating problem, and this was the best I could come up with. Hope it can help.