Modal View Controller force Landscape orientation in iOS 6
Asked Answered
S

4

8

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.

Systemic answered 21/8, 2012 at 18:4 Comment(2)
from my previous experience with this, there is no way to force a viewController to appear in a specific orientation. You can choose whether you allow the orientation to change or not, but not force it. it was a bummer for me...Mercury
Thats a bit stupid really! I have a graph that will only fit in Landscape. So you saying Apple wants me to show it screwed up until the user turns his device. That doesn't sound right, although I haven't found a solution so you may be right.Systemic
B
7

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.

Breathing answered 3/10, 2012 at 20:24 Comment(0)
K
6

If you want to force rotation in iOS6, your rootviewController should implement these methods:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL)shouldAutorotate {
    return YES;
}
Kneecap answered 3/10, 2012 at 8:44 Comment(0)
C
1

In iOS 6 it seems to be this method may help force a specific orientation on iOS 6.

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

I am still trying to get it to work and will update with any findings.

Cope answered 24/9, 2012 at 19:7 Comment(1)
Thanks, it works in my case together with the method -(BOOL)shouldAutorotate{ return NO;}Overrefinement
U
0

I do exactly this in my app, and do it via shouldAutoRotateToInterfaceOrientation, slightly differently than you:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
  return (YES);
 return (NO);
}

for iOS 6, you must also set window.rootViewController = {your root view controller}; in your app delegate. I'm hoping this is your issue

Unify answered 21/8, 2012 at 18:14 Comment(4)
This still doesn't force the orientation. I've added this to my AppDelegate self.window.rootViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"mainTab"];Systemic
ok bummer, I'm not using storyboards in my apps, though I wouldn't think that should cause this issue. I suspect the rootViewController property is being set for you automatically anyhow. It would be interesting to log calls to shouldAutorotate to see if its even getting calledUnify
No, shouldAutorotate isn't being called at all!Systemic
Oops. shouldAutorotateToInterfaceOrientation is deprecated in iOS 6. I guess I should have mentioned I was using it.Systemic

© 2022 - 2024 — McMap. All rights reserved.