Autorotate a single UIViewController in iOS 6 with UITabBar

Piero picture Piero · Sep 23, 2012 · Viewed 25.6k times · Source

I have an app that work only in Portrait Mode, but there is a singleView that can display video, so i want that view work also in the landscape mode, but in iOS 6 I can't figure out how I can do it, now I have this:

In AppDelegate.m i have:

self.window.rootViewController = myTabBar;

then in the Summary of the project:

enter image description here

and i found that in iOS 6 to detect the view rotation i have to do this:

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}

// Tell the system It should autorotate
- (BOOL) shouldAutorotate {
return YES;
}

so i insert the code above only in my UIViewController that I want use also in landscape, but don't work, anyone knows how i can do it? i just want the autorotate when show video.

Answer

Michal Zaborowski picture Michal Zaborowski · Sep 23, 2012

Firstly, your target settings should look like this: Supported Interface Orientations

In UITabBarController:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

-(NSUInteger)supportedInterfaceOrientations
{
    if (self.selectedViewController) 
        return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

Inside your ViewController:

a) if you dont want to rotate:

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

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

b) if you want to rotate to landscape:

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

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

Edit:

Other solution is to implement this method inside AppDelegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations = UIInterfaceOrientationMaskAll;

    if (self.window.rootViewController) {
        UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presented supportedInterfaceOrientations];
    }
    return orientations; 
}