ModalViewController rotation issues within TabBarController
I can't quite see why I'm having this issue but I am… Basically I have a Tabr bar controller with a navigationController
in one of the tabs. In this particular tab I want the user to be able to rotate the device and see a completely different view, not the original view rotated!
In order to achieve this (at full screen) I have a LandscapeViewController
which I present modally when the device is in landscape orientation. The problem occurs when I present the modalViewController
…
In the LandscapeViewController
I also check the orientation so I can dismiss self when the user goes back into portrait mode. However when I present it I get a callback saying it's going into portrait mode, which completely messes it up!
Here's the code and log statements to clarify what's going on..
PortraitViewController
(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
NSLog(@"Norm going Portrait");
[self changeTheViewToPortrait:YES andDuration:duration];
}
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
NSLog(@"Norm going Landscape");
[self changeTheViewToPortrait:NO andDuration:duration];
}
}
(void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:5.];
if(portrait){
NSLog(@"dismissed");
[self dismissModalViewControllerAnimated:NO];
开发者_StackOverflow中文版 }
else{
NSLog(@"presented");
LandscapeOverviewViewController *land = [[LandscapeOverviewViewController alloc]initWithNibName:@"LandscapeOverviewViewController" bundle:nil];
land.delegate = self;
[self presentModalViewController:land animated:NO];
[land release];
}
[UIView commitAnimations];
}
LandscapeViewController
(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
NSLog(@"Modal is going Portrait");
[self changeTheViewToPortrait:YES andDuration:duration];
}
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
NSLog(@"Modal is going Landscape");
[self changeTheViewToPortrait:NO andDuration:duration];
}
}
(void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
if(portrait){
NSLog(@"calling delegate to dismiss");
[delegate landscapeViewController:self didRotateBack:(YES)];
}
[UIView commitAnimations];
}
and this is the log text:
Norm going Landscape
2010-08-24 15:29:40.024 App[32461:207] presented
2010-08-24 15:29:40.026 App[32461:207] Modal is in going Portrait
2010-08-24 15:29:40.026 App[32461:207] calling delegate to dismiss
2010-08-24 15:29:40.027 App[32461:207] delegate called
As you can see when in portrait and I rotate it does the correct thing by presenting the landscapevc
, but then it thinks it's in portrait and tries to dismiss.
Can anyone see where I'm going wrong? (And apoligies for the length of this but the formatting of code wouldn't work properly otherwise.)
Have you implemented the shouldAutorotateToInterfaceOrientation: method in the landscape view controller? If not, this view controller will be forced to stay in portrait mode, which will in turn trigger your dismissal code.
It's also worth noting that sometimes view controllers are added to a window in a certain orientation, and then rotated. It's worth it to check the orientation that willRotate passes to you against your current rotation. I haven't verified this in quite some time, but in the late 2.x / early 3.0 days, this was common.
精彩评论