My app accepts only portrait orientation, except for MPMoviePlayerController
, which I want to allow user to change orientation.
So, I subclassed UINavigationController
and added the following code:
-(BOOL)shouldAutorotate
{
if ([[[self.viewControllers lastObject] presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
return YES;
}
else
{
return NO;
}
}
It works great, allowing only my MPMoviePlayerViewController
to change orientation. The problem is that when user is in landscape orientation and presses the done
button or playback ends, it pops the moviePlayer
and goes back to the presentingViewController, but on landscape mode, causing a crash, since that view is not made for landscape orientation.
I tried a few things to change back to Portrait
but had no luck. I'm using storyboards, if that makes any difference. I would like to change the orientation back to portrait on viewWillAppear
, or maybe getting the done
button press and change the orientation there.
UPDATE:
Here is the updated code in my UINavigationController
subclass:
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
if ([[[self.viewControllers lastObject] presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
return UIInterfaceOrientationMaskAll;
}
else
{
return UIInterfaceOrientationMaskPortrait;
}
}
Now, if I do things in the following order from the view with the Play button
.
- Rotate the device. (it calls the method but screen doesn't rotate since it is not
MPMoviePlayerController
class) - Press the play
button
. (It presents the player already on landscape mode). - Press the back button. (It pops the player and correctly shows the view on portrait mode)
Now, if I change the order to:
- Press the play button. (holding the device on the regular portrait position).
- Rotate the device. (it rotates the movie player correctly showing the video).
- Press the back button. (it pops the player, but this time, the view is in landscape mode, which is not the expected behavior)