sirab333
sirab333

Reputation: 3722

Tab-Bar App Crashes on Device Rotation in iOS6

THIS IS NOT A DUPLICATE QUESTION. A final working solution has NOT been provided yet so please do not close this question until we've accepted an answer or found and provided our own 100% working solution. Thanks!

==================================================================
Using Xcode 4.5.1, we have a tab-bar app with 5 tabs in it. Each tab contains a UINavigationController, so the entire App thus needs to be viewed in Portrait mode. There is one exception: an "image-gallery" type view controller that we need to open and be viewed full-screen, and in LANDSCAPE mode.

We were able to do this easily in iOS5 using the following code in that one particular ViewController:

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

But shouldAutorotateToInterfaceOrientation has been deprecated in iOS6 and doesn't work any more.
So, to get this to work in iOS6, we've taken the following steps so far:
1) created a subclass of the UITabBarController (which is our rootViewController)
2) set its supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation to UIInterfaceOrientationMaskPortrait (But note that we are NOT implementing the shouldAutorotate method in it)
3) Set the PROJECT/Target supported orientations to ALL

This ALMOST works perfectly: our "Image Gallery" ViewController does respond to both landscape modes - as it should - but it still initially opens in Portrait - which is bad. We need it to open up right in Landscape - and not ever be able to be displayed in Portrait. Right now it still doing both.

Any idea why its doing that - or how to fix it?

Upvotes: 3

Views: 531

Answers (2)

Mohammad Rabi
Mohammad Rabi

Reputation: 1412

shouldAutorotateToInterfaceOrientation method is deprecated in iOS 6

try to implements these following methods.

-(BOOL)shouldAutomaticallyForwardAppearanceMethods{   
     // This method is called to determine whether to 
     // automatically forward appearance-related containment
     //  callbacks to child view controllers.
}
-(BOOL)shouldAutomaticallyForwardRotationMethods{
    // This method is called to determine whether to
    // automatically forward rotation-related containment 
    // callbacks to child view controllers.
}

note : these methods just supported in iOS 6.

Upvotes: 1

Suhail Patel
Suhail Patel

Reputation: 13694

I had this exact same problem with an app I work on and this is how I solved it.

First I created a subclass of UITabBarController called NonRotatingTabBarController with the portrait orientation code

NonRotatingTabBarController.h

#import <UIKit/UIKit.h>

@interface NonRotatingTabBarController : UITabBarController
@end

NonRotatingTabBarController.m

#import "NonRotatingTabBarController.h"

@implementation NonRotatingTabBarController

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

@end

Now when you create your Tab Bar Controller, it needs to be an instance of NonRotatingTabBarController

self.tabBarController = [[NonRotatingTabBarController alloc] init]; // or whatever initialising code you have but make sure it's of type NonRotatingTabBarController

Now in the ONLY view controller which needs to have landscape support, you need to override the rotation methods so it does rotate. In my case, it had to be fixed to landscape

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return (UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight);
}

- (NSUInteger)supportedInterfaceOrientations 
{
    return UIInterfaceOrientationMaskLandscape;
}

In your project/target settings, you MUST have support enabled for all the interface orientations your app uses otherwise it will crash. Let the code above take care of rotation enabling/disabling.

Xcode Project Settings

Hope that helps!

Upvotes: 1

Related Questions