Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - allow landscape orientation on just one viewcontroller

I have a navigation-based application in which I would like just one of the viewcontrollers to support landscape orientation. For that viewcontroller (vc1), in shouldAutorotate, I am returning YES for all orientations and in the other controllers I am returning YES only for portrait mode

But even then if the device is in landscape mode and I go to the next screen from vc1, the next screen also comes up rotated in landscape mode. I assumed that if I return a YES only for portrait mode, the screen should show up only in portrait.

Is this the expected behavior? How do I achieve what I am looking for?

Thanks.

like image 427
lostInTransit Avatar asked Jan 27 '10 04:01

lostInTransit


People also ask

How do I lock rotation on landscape?

Here's how you do it: On your phone, swipe down from the top of the display twice to expand the notification shade to see all quick toggles, tap the text that says Auto rotate (or Portrait or Landscape, in case auto rotate is off), and then enable the toggle switches next to Lock screen and Voice call screen.

How do I force landscape mode in iOS programmatically?

Use iOS's Built-In AssistiveTouch Feature You can use the AssistiveTouch iOS feature to force-rotate your iPhone's screen into landscape mode, even if you're using the device in portrait mode. This is the only built-in way to force-rotate your iPhone screen.


2 Answers

I have also situation when I need all view controllers in portait mode, but one of them also can rotate to landscape mode. And this view controller has navigation bar.

For this purpose I have created second window, in my case it was camera view controller. And when I need to show camera view controller, I show camera window and hide when I need to push another view controller.

And you also need to add this code to AppDelegate.

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{   
    if (window == self.cameraWindow)
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }

    return UIInterfaceOrientationMaskPortrait;
}
like image 160
Salo Ievgen Avatar answered Oct 09 '22 11:10

Salo Ievgen


You can't support the landscape orientation only for one of the viewcontrollers if you use shouldAutorotateToInterfaceOrientation method of UIViewController.
You have only two choice whether all viewcontrollers support the landscape or no viewcontrollers support it.

If you want to support the landscape only for one, You need to detect device rotation and manually rotate views in the viewcontroller.
You can detect the device rotation by using Notification.

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didRotate:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

Then, you can rotate your views when you detect the device rotation.

- (void)didRotate:(NSNotification *)notification {
    UIDeviceOrientation orientation = [[notification object] orientation];

    if (orientation == UIDeviceOrientationLandscapeLeft) {
        [xxxView setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
    } else if (orientation == UIDeviceOrientationLandscapeRight) {
        [xxxView setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
    } else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
        [xxxView setTransform:CGAffineTransformMakeRotation(M_PI)];
    } else if (orientation == UIDeviceOrientationPortrait) {
        [xxxView setTransform:CGAffineTransformMakeRotation(0.0)];
    }
}
like image 25
tomute Avatar answered Oct 09 '22 11:10

tomute