Question

i need some help.

I'm currently doing an application in xcode where i need to have all views locked in portrait mode, but i have a second view controller that needs to go in landscape because i'm using mediaplayer framework to show a video and that needs to be in landscape.

I have been searching for this, for around a week now and still can't solve the problem.

I'd appreciate the help thanks in advance.

Was it helpful?

Solution

This has two aspects. 1. How to lock the orientation for each view controller as requierd.

But that alone does not rotate the device. When you are in portrait and the next view controller to be shown is for landscape, then it would still be presented in portrait. You could turn the device to portait and the conroller would rotate accordingly. Once in landscape it is fixed in landscape and cannot be rotated any more. When you go back then to your portrait controller, it would be presented in landscape ...

So, 2. you need to rotate the device.

Pont 1 is easy.

Implement this in all controllers that are fix for portrait:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

And implement this for the landscape controllers:

- (BOOL)shouldAutorotate
{
    return YES;
}


- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

2. For the next step you need to apply a trick. You cannot programatically turn the device. The only way achieving that is rotating the status bar. After that the next modal (!) view controller is presented in the new orientation. This does not work for pushing controllers on the navigation stack. The trick is to present any (empty) view controller modally and remove it straight away. Now the device is turned and you can push a view controller to the stack. This is the code, that I used the other day:

// Fetch the status bar from the app and set its orientation as required. 
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

// Create an empty view controller, present it modally and remove it directly afterwards
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
// Now the device is rotated to the desired orientation. Go from there.

If you are working with modal view controllers anyway, then it is a bit simpler of course.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top