views:

397

answers:

1

I have a view with a tableView and a mapView. In portrait mode, I want the mapView to occupy the top 60% of the screen, and the table view, the bottom 40%. In landscape, I want the mapView to occupy 45% of the screen on the right, and the tableView 55% on the left.

Thus, I thought that something like this ought to work:

    -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
     UIScreen* mainscr = [UIScreen mainScreen];
     CGSize screenSize = mainscr.currentMode.size;

 if(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft 
    || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
 {
  tableview.frame = CGRectMake(0, 0 , (screenSize.height*.55), screenSize.width);
  mapView.frame = CGRectMake((screenSize.height*.55), 0, (screenSize.height*.45), screenSize.width);
 }
 else 
 {
  tableview.frame = CGRectMake(0, (screenSize.height*.6) ,screenSize.width, (screenSize.height*.4));
  mapView.frame = CGRectMake(0, 0,screenSize.width, (screenSize.height*.6));
 }

    }

This code works fine on the iphone simulator with version 4 of the OS, but when I try to run it on the iphone 4 simulator or the ipad simulator, the views are completely off (i can only see the about a quarter of the map in portrait and none of the table, and in landscape i can see only the table).

I dont understand why this is the case -- since im simply scaling the views to match the screen size, shouldnt something like the above code work fine regardless of the resolution of the device? I was able to make a 'fix' by scaling using different values for the iphone, iphone 4, and ipad, but the numbers that I used were discovered by trial and error, and they dont appear to make much sense.

can someone please point me towards the correct method for handing these orientation changes for all devices? Thanks so much!

+3  A: 

I have done something similar to this. Here is how I would accomplish what you would like to do

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    CGSize screenSize = self.view.bounds.size;

    [UIView beginAnimations:@"InterfaceRotations" context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:duration];

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        tableview.frame = CGRectMake(0, 0 , (screenSize.height*.55), screenSize.width);
        mapView.frame = CGRectMake((screenSize.height*.55), 0, (screenSize.height*.45), screenSize.width);
    } else {
        tableview.frame = CGRectMake(0, (screenSize.height*.6) ,screenSize.width, (screenSize.height*.4));
        mapView.frame = CGRectMake(0, 0,screenSize.width, (screenSize.height*.6));
    }

    [UIView commitAnimations];
}
rickharrison
Thanks, this works really well. One point to make though, is that if the orientation is landscape, you have to flip 'height' and 'width' everywhere that its specified. Otherwise, its perfect.
culov