IPhone 3, iPhone 4, and iPad orientation handling
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 开发者_JS百科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!
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];
}
精彩评论