Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modal View Controller force Landscape orientation in iOS 6

I have a UITabBarController presented in Portrait mode. On one of the tabs I have a button that shows a UIViewController modally (A simple storyboard segue performs the action).

I want this modal view to be shown in Landscape mode, but I can't get it to turn automatically.

I have this in the modal views controller

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

I've added landscapeLeft to the .plist supported orientations (although this also allows the TabBar to be rotated which I don't want)

I've also added this to the ViewDidLoad of the modal view

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;

but I just can't get it to rotate by itself.

Thanks

EDIT ----

It seems shouldAutoRotate isn't even being called!

Also, I'm trying to detect the orientation and this code below always shows 2, regardless of orientation!

if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait) {
        NSLog(@"1");
        self.hostView.frame = CGRectMake(0, 60, 200, 255);
    } else {
        NSLog(@"2");
        self.hostView.frame = CGRectMake(0, 45, 480, 255);
    }

EDIT 2 ---

My bad. I guess I should have mentioned I was using iOS 6. The display rotates automatically on iOS 5. shouldAutorotateToInterfaceOrientation is deprecated so I need to read up on the new methods.

like image 974
Darren Avatar asked Aug 21 '12 18:08

Darren


People also ask

How do you force a landscape orientation?

When on the main screen, under the orientation section, you will see a number of options like 'Auto-rotate OFF', 'Auto-rotate ON', 'Forced Portrait' and 'Forced Landscape'. As the names suggest, you can use these buttons as one-tap shortcuts to toggle the orientation of your device.


1 Answers

iOS 6.0 Release Notes
"More responsibility is moving to the app and the app delegate. Now, iOS containers (such as UINavigationController) do not consult their children to determine whether they should autorotate. "

-(BOOL)shouldAutorotate of UIViewController under any Container (like UINavigationController) will not be called from IOS6.

Tried a solution with using Category to add method to existing UINavigationController Class.

inside the shouldAutorotate method, you can call shouldAutorotate method in each view controller of self.viewControllers. In fact, you can pass to your child view controller.

like image 56
Esmond Avatar answered Sep 22 '22 08:09

Esmond