orientation reported correctly, but wrong bounds

Discussion in 'iOS Programming' started by guivho, Sep 8, 2010.

  1. guivho macrumors newbie

    Joined:
    Sep 8, 2010
    #1
    Hi,

    I want my views to be drawn correctly in all orientation modes. So I pick-up the view bounds and the device orientation in the didRotateFromInterfaceOrientation: method. I would expect that the width would be less than the height for all landscape modes and vice versa for portrait modes. This is not always true, and I do not know what I am doing wrong.

    I use following macro to ease my logging to the console:
    Code:
    // Simple macro to pick up functionname and linenr
    #define MCHERE  [NSString stringWithFormat:@"%s@%d", __FUNCTION__, __LINE__]
    
    // MCLog is a simple macro that runs NSLog with location info
    #define MCLog NSLog(@"%@", MCHERE)
    #define MCLog4(FORMAT, ARG1, ARG2, ARG3, ARG4) NSLog([MCHEREC stringByAppendingString:FORMAT], ARG1, ARG2, ARG3, ARG4)
    
    My didRotateFromInterfaceOrientation: method is as follows:
    Code:
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    	CGRect aRect = [self.view bounds];
    	UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    	BOOL landscapeMode = orientation == UIDeviceOrientationLandscapeLeft
                || orientation == UIDeviceOrientationLandscapeRight;
    	BOOL portraitBoundsGiven = aRect.size.width < aRect.size.height;
    	MCLog4(@"%f x %f orientation:%d %@",aRect.size.width, aRect.size.height,
                orientation, portraitBoundsGiven && landscapeMode ? @"ERROR!" : @"OK");
    	[self setAndDrawUserInterface];
    }
    
    Note that this UIViewController is pushed by a UITableViewController navigationController.

    And here's an excerpt from the console output while I'm just rotating the iPhone Simulator. Note that often the device claims to be in landscape mode (3 or 4) but reports a width that is less than the reported height. That is definitely an error:

    Code:
    [Session started at 2010-09-08 19:25:03 +0200.]
    2010-09-08 19:25:09.872 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:480.000000 x 268.000000 orientation:4 OK
    2010-09-08 19:25:11.995 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:480.000000 x 268.000000 orientation:1 OK
    2010-09-08 19:25:12.325 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:320.000000 x 416.000000 orientation:4 ERROR!
    2010-09-08 19:25:12.657 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:480.000000 x 268.000000 orientation:4 OK
    2010-09-08 19:25:15.666 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:480.000000 x 268.000000 orientation:1 OK
    2010-09-08 19:25:15.997 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:320.000000 x 416.000000 orientation:4 ERROR!
    2010-09-08 19:25:16.328 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:480.000000 x 268.000000 orientation:2 OK
    2010-09-08 19:25:17.338 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:480.000000 x 268.000000 orientation:1 OK
    2010-09-08 19:25:17.669 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:320.000000 x 416.000000 orientation:4 ERROR!
    2010-09-08 19:25:18.000 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:480.000000 x 268.000000 orientation:2 OK
    2010-09-08 19:25:18.812 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:480.000000 x 268.000000 orientation:1 OK
    2010-09-08 19:25:19.143 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:320.000000 x 416.000000 orientation:1 OK
    2010-09-08 19:25:19.511 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:480.000000 x 268.000000 orientation:4 OK
    2010-09-08 19:25:20.596 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:480.000000 x 268.000000 orientation:1 OK
    2010-09-08 19:25:20.928 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:320.000000 x 416.000000 orientation:4 ERROR!
    2010-09-08 19:25:21.260 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:480.000000 x 268.000000 orientation:2 OK
    2010-09-08 19:25:22.167 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:480.000000 x 268.000000 orientation:1 OK
    2010-09-08 19:25:22.499 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:320.000000 x 416.000000 orientation:4 ERROR!
    2010-09-08 19:25:23.976 App[10306:207] -[AppViewController didRotateFromInterfaceOrientation:]@347:480.000000 x 268.000000 orientation:3 OK
    
    Note that sporadically the iPhone claims to be in potrait mode, but reports landscape bounds.

    This definitely makes it difficult to draw my view in the correct orientation mode.

    Does anyone has an explanation for this unfortunate phenomenon?

    Any help and/or suggestion would be very welcome!

    Guivho.
     
  2. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    I think if you use layoutSubviews things will work more reliably.
     

Share This Page