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)
I have a working configuration for your problem, as far as I understand from your question.
You need a subclass of UINavigationController as you already have, but with the following code:
The .h file:
The .m file:
Now, in the view controllers that will be under this navigation controller that you want to be just portrait you just need to set the "landscapeDisallowed" to YES in the viewDidLoad method, like:
And override the following methods like this:
The views that can be presented in landscape mode just need to:
It works for me, hope it works for you.
Hope this will help
Found a solution here on this answer.
On button click, I added a notification and then on done button click I used the below code to change orientation. I kept the
UINavigationController
subclass as in my question to allow the change of orientation when movie starts playing.