Reputation: 4758
I'd like to show a different full screen view when the iOS device is rotated to a landscape orientation and return to the previous view when the device is rotated back to landscape.
I've mostly gotten it to work by using one view controller and two views, then setting the view controller's self.view in the - shouldAutorotateToInterfaceOrientation: to the appropriate view.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight))){
self.view = landscapeView;
}else if(((interfaceOrientation == UIInterfaceOrientationPortrait) ||
(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){
self.view = portraintView;
}
return YES;
}
However, ideally I would like the landscape view to have it's own separate view controller to manage the view. I've tried to push the view controller modally and dismiss it in the shouldAutorotateToInterfaceOrientation:, but the landscape view controller doesn't come up in the correct orientation (it still thinks the device is in portrait)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight))){
[self presentModalViewController:landscapeViewController animated:YES];
}else if(((interfaceOrientation == UIInterfaceOrientationPortrait) ||
(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){
[self dismissModalViewControllerAnimated:YES];
}
return YES;
}
Then when I go back to the portrait view it still thinks the device is in landscape.
Upvotes: 1
Views: 1535
Reputation: 4758
As @MishieMoo pointed out, I needed to do my work in didRotateToInterfaceOrientation so that the view controller would get presented in the correct orientation.
So now the code for my portrait view controller looks like this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if(fromInterfaceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationPortraitUpsideDown == UIInterfaceOrientationLandscapeRight){
[self performSegueWithIdentifier:@"fullscreenSegue" sender:self];
}
}
I do a storyboard segue to push the full screen view controller, but you could just as easily load the view controller and do [self presentModalViewController:landscapeViewController animated:YES].
And the code to dismiss the view in the full screen view controller:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
[self dismissModalViewControllerAnimated:NO];
}
}
Upvotes: 0
Reputation: 6680
You should be doing your rotation work in willAnimateRotationToInterfaceOrientation: duration:
or didRotateToInterfaceOrientation:
, not shouldRotateToInterfaceOrientation
. Then use the supplied interfaceOrientation
to switch out your views. This way is much more reliable and only gets called when you're actually rotating the device.
Upvotes: 1