Shouldautorotatetointerfaceorientation Not Being Called in iOS 6

iOS 6 shouldAutorotate: is NOT being called

See if you are getting the following error when your App starts.

Application windows are expected to have a root view controller at the end of application launch

If so the way to fix it is by making the following change in the AppDelegate.m file (although there seem to be a number of answers how to fix this):

// Replace
[self.window addSubview:[navigationController view]]; //OLD

// With
[self.window setRootViewController:navigationController]; //NEW

After this shouldAutoRotate should be correctly called.

shouldAutorotateToInterfaceOrientation is not working in iOS 6

EDIT: This is happening because Apple has changed the way of managing the Orientation of UIViewController. In iOS6 orientation handles differently. In iOS6 shouldAutorotateToInterfaceOrientation method is deprecated. View controllers (such as UINavigationController) do not consult their children to determine whether they should autorotate. By default, an app and a view controller’s supported interface orientations are set to UIInterfaceOrientationMaskAll for the iPad idiom and UIInterfaceOrientationMaskAllButUpsideDown for the iPhone idiom.

If you want a specific view to be changed to the desired orientation you have to do some sort of subclass or category and override the autorotation methods to return the desired orientation.

Place this code in your root view controller. This will help the UIViewController to determine its orientation.

  //RotationIn_IOS6 is a Category for overriding the default orientation.

@implementation UINavigationController (RotationIn_IOS6)

-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end

Now you need to implement below methods (introduced in iOS6) in viewController for orientation

- (BOOL)shouldAutorotate
{
//returns true if want to allow orientation change
return TRUE;


}
- (NSUInteger)supportedInterfaceOrientations
{
//decide number of origination tob supported by Viewcontroller.
return UIInterfaceOrientationMaskAll;


}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
//from here you Should try to Preferred orientation for ViewController
}

And put your code inside the below method. Whenever device orientation is changed
this method will be called:

 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)  interfaceOrientation duration:(NSTimeInterval)duration
{
if([[[SampleApplicationAppDelegate instance].callInfoDictionary valueForKey:IS_CHAT] isEqualToString:NO_RESPONSE])
{
int nAngle = 0;
BOOL bRet = NO;

switch (interfaceOrientation) {
case UIInterfaceOrientationPortrait:
nAngle = 90;
bRet = YES;
NSLog(@".......Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height);

_previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);

NSLog(@"Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height);
break;

case UIInterfaceOrientationPortraitUpsideDown:
nAngle = 270;
bRet = YES;
_previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);
break;

case UIInterfaceOrientationLandscapeLeft:
nAngle = 0;
bRet = YES;
//_previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);
break;

case UIInterfaceOrientationLandscapeRight:
nAngle = 180;
bRet = YES;
//_previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);
break;

default:
break;
}
}

Edit: check your window, you need to add the controller on window as rootViewController rather than addSubview like below

self.window.rootViewController=viewController;

For more information here's an article about iOS6.0 Beta 2 OTA.

I hope this was helpful.

iOS 6: supportedInterfaceOrientations is called but shouldAutorotate is not called

If you add these to your root view controller:

- (BOOL)shouldAutorotate
{
NSLog(@"ViewController shouldAutorotate super=%d", [super shouldAutorotate]);
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
NSLog(@"ViewController supportedInterfaceOrientations");
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

you will see that the system is constantly sending both of these messages. Your code there can query the active Navigation Controller to see what values to return if you wish.

Also, I'm no fan of using a category to override these on a UINavigation Controller - that it works is somewhat of a fluke and importing some other library that does the same could cause unexpected consequences later on. Just create a really simple subclass and over ride both of these. I've been doing that with both UITabBarController and UINavigationController since iOS4 and never had an issue.

supportedInterfaceOrientations method not working on iOS 6 UIViewController

It's how I was adding my viewController.

I replaced this line code:

[window addSubview:viewController.view];

by this line:

[window setRootViewController:viewController];

iOS6: supportedInterfaceOrientations not working (is invoked but the interface still rotates)

If your are using a UINavigationController as the root window controller, it will be its shouldAutorotate & supportedInterfaceOrientations which would be called.

Idem if you are using a UITabBarController, and so on.

So the thing to do is to subclass your navigation/tabbar controller and override its shouldAutorotate & supportedInterfaceOrientations methods.

Where to define the -shouldAutorotate method in iOS 6?

OK, solved. What I did: In my topmost navigation controller I defined

-(BOOL)shouldAutorotate {return [self.visibleViewController shouldAutorotate];}

and then, in my next viewController

-(BOOL)shouldAutorotate {return NO;}

Thanks guys!!!

IO6 doesn't call -(BOOL)shouldAutorotate

It's because neither UITabBarcontroller nor UINavigationController is passing shouldAutorotate to its visible view controller. To fix that you may subclass either UITabBarController or UINavigationController and forward shouldAutorotate from there:

In your subclassed UITabBarController add:

- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
}

In your subclassed UINavigationController add:

- (BOOL)shouldAutorotate
{
return [self.visibleViewController shouldAutorotate];
}


Related Topics



Leave a reply



Submit