Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Incorrect" frame / window size after re-orientation in iPhone

In my iPhone OS application I want (need) to watch for changes in the device orientation in order to rearrange certain portions of the screen. The approach I used was to use CGRect frame = [UIScreen mainScreen].applicationFrame to get the screen size, and from there calculate the size and / or positioning of other controls (I also tried self.view.frame).

All testing was done so far in Portrait mode, so I could focus on programming the main features and later on just do some adjustments for Landscape. And here enters the problem: In -(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation I added some logging to check the sizes before proceeding, but apparently the values for Width and Height are "wrong" (I say "wrong" because at a first glance the values does not make sense to me).

Here's the output of some logging:

  • Rotation: Landscape [w=300.000000, h=480.000000]
  • Rotation: Portrait [w=320.000000, h=460.000000]

The values for "w" and "h" in Landscape seem inverted to me - I was expecting that w=480 and h=300.

What am I doing wrong? The code I used to debug is below.

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    CGSize size = frame.size;

    NSLog(@"%@", [NSString stringWithFormat:@"Rotation: %s [w=%f, h=%f]",  
        UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? "Portrait" : "Landscape",
        size.width, size.height]);
}
like image 774
Rafael Steil Avatar asked Apr 21 '10 22:04

Rafael Steil


People also ask

How do I reset the orientation on my iPhone?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off. Turn your iPhone sideways.

How do I force horizontal orientation in IOS?

On an iPhone with a Home button, swipe up from the bottom of the screen to access it. On an iPhone without a Home button, swipe down from the top-right corner of the screen instead. Here, tap on the rotation lock icon (which looks like a lock with a circular arrow) to turn it on or off.


2 Answers

The orientation of your device changed, not the physical characteristics of the screen. You basically tipped it on its side, but in reality it is 320 pixels wide (20 of which are not available to you at the moment since the status bar is showing) and 480 pixels tall. If your view is auto-rotating, then the width/height have been translated for you, but when you ask for the actual dimensions of the screen, you get back the actual dimensions of the screen.

This is also why, when working with translated views, it is important to do calculations based on the view's center and the view's bounds and never on the view's frame.

like image 132
Jason Coco Avatar answered Sep 17 '22 12:09

Jason Coco


Use self.view.bounds instead.

like image 29
Mike Keskinov Avatar answered Sep 21 '22 12:09

Mike Keskinov