IOS6: supportedInterfaceOrientations not working (called, but the interface still rotates)

I have several views in my application, some views should support both portrait and landscape, while other views should only support portrait. Thus, in the project summary, I chose all orientations.

The following code worked to disable landscape mode on this view controller prior to iOS 6:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } 

Since shouldAutorotateToInterfaceOrientation is deprecated in iOS6, I replaced above:

 -(NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMask.Portrait; } 

This method is correctly called when the view appears (I can set a breakpoint to ensure this), but the interface still rotates in landscape mode, regardless of the fact that I return the mask only for portrait modes. What am I doing wrong?

It seems that currently it is not possible to create an application that has different orientation requirements for each view. It seems that they adhere only to the orientation indicated in the project summary.

+49
ios objective-c iphone ios6
Sep 16 '12 at 14:10
source share
15 answers

If you use the UINavigationController as the root window controller , it will be shouldAutorotate and supportedInterfaceOrientations , which will be called.

Same if you use UITabBarController etc.

So, you need to make sure to subclass your navigation / tab controller and override its shouldAutorotate and supportedInterfaceOrientations methods.

+69
Sep 21 '12 at 10:07
source share

try changing this code in AppDelegate.m

 // self.window.rootViewController = self.navigationController; [window setRootViewController:navigationController]; 

this is the full answer

shouldAutorotateToInterfaceOrientation not called in iOS 6

Xd

+30
Sep 20
source share

In my case, I have a UINavigationController and my view controller inside. I had to subclass the UINavigationController and, to support portrait only, add this method:

 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; } 

So, in a subclass of UINavigationController, I need to check which orientation is supported by the current topViewController.

 - (NSUInteger)supportedInterfaceOrientations { return [[self topViewController] supportedInterfaceOrientations]; } 
+20
Sep 20 '12 at 13:25
source share

One thing I found is if you have an old application that still does

 [window addSubView:viewcontroller.view]; //This is bad in so may ways but I see it all the time... 

You will need to update it to:

 [window setRootViewController:viewcontroller]; //since iOS 4 

Once you do this, the orientation will start working again.

+15
Sep 20 '12 at 18:29
source share

The best way for iOS6 is defined in "iOS6 By Tutorials" by the Ray Wenderlich team - http://www.raywenderlich.com/ and better than the UINavigationController subclasses for most cases.

I am using iOS6 with a storyboard that includes a UINavigationController installed as the initial view controller.

//AppDelegate.m - this method is not available until iOS6, unfortunately

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown; if(self.window.rootViewController){ UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject]; orientations = [presentedViewController supportedInterfaceOrientations]; } return orientations; } 

//MyViewController.m - return any orientations that you want to support for each UIViewController

 - (NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskPortrait; } 
+14
Dec 21 '12 at 12:30
source share

As pointed out by others, if you use a UINavigationController and want to customize different views, you will want to subclass the UINavigationController and make sure that you have these two components:

 @implementation CustomNavigationController // ------------------------------------------------------------------------------- // supportedInterfaceOrientations: // Overridden to return the supportedInterfaceOrientations of the view controller // at the top of the navigation stack. // By default, UIViewController (and thus, UINavigationController) always returns // UIInterfaceOrientationMaskAllButUpsideDown when the app is run on an iPhone. // ------------------------------------------------------------------------------- - (NSUInteger)supportedInterfaceOrientations { return [self.topViewController supportedInterfaceOrientations]; } // ------------------------------------------------------------------------------- // shouldAutorotate // Overridden to return the shouldAutorotate value of the view controller // at the top of the navigation stack. // By default, UIViewController (and thus, UINavigationController) always returns // YES when the app is run on an iPhone. // ------------------------------------------------------------------------------- - (BOOL)shouldAutorotate { return [self.topViewController shouldAutorotate]; } 

Then in any view that is only a portrait, you should include:

 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

And in any representation, this is all but inverted:

 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAllButUpsideDown; } 
+10
May 14 '13 at 6:30 a.m.
source share

In principle, as indicated above, but in more detail:

  • Create a new file that is a subclass of UINavigationController
  • Go to your storyboard and then click on the navigation controller, set its class to the one you just created.
  • In this class (.m file) add the following code so that it stays in portrait mode:

     (BOOL)shouldAutorotate { return NO; } (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

It worked for me

+2
Feb 17 '13 at 22:25
source share

This code worked for me:

 -(BOOL)shouldAutorotate { return YES; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAll; } 

Orientation iPhone / iPad App check my own answer

+1
Sep 22 '12 at 3:23
source share

The best way, I think, is to make a category and not subclass UINavigationController or UITabbarController

your UINavigationController + Rotation.h

 #import <UIKit/UIKit.h> @interface UINavigationController (Rotation) @end 

your UINavigationController + Rotation.m

 #import "UINavigationController+Rotation.h" @implementation UINavigationController (Rotation) -(BOOL)shouldAutorotate { return [[self.viewControllers lastObject] shouldAutorotate]; } -(NSUInteger)supportedInterfaceOrientations { return [[self.viewControllers lastObject] supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; } @end 

Try to have your entire controller import this category, and this work like a charm. You can even make the controller not rotate and by pressing another controller that will rotate.

+1
Oct 30 '12 at 16:22
source share

Try adding the shouldAutorotate method

0
Sep 17 '12 at 8:28
source share

First, for your application to work only in mode, you must return a UIInterfaceOrientationMaskLandscape . If you want to keep only portrait mode, you are doing everything right.

Just add the UISupportedInterfaceOrientations key to Info.plist and assign the interface orientation values ​​that your application intends to save.

In addition, you must return false from shouldAutoRotate if you want to completely avoid automatic rotation. But I suggest you return from here and specify the correct orientations in the supportedInterfaceOrientations method.

0
Sep 17 '12 at 9:26
source share

I have the same situation as you. I know that you have already accepted the answer, but I thought I would add another one. So I understand that the new version of the rotation system works. The root view controller is the only view controller that will ever be called. I believe that the argument is that with child controllers it often does not make sense to rotate your views, since they will still remain within the root view controller.

So what is going on. The first shouldAutorotate is called on the root view controller . If NO returns, everything stops. If YES returned, the supportedInterfaceOrientations method is called. If the orientation of the interface is confirmed in this method and globally supported orientations from Info.plist or the application delegate, then the view will rotate. Before rotation, the shouldAutomaticallyForwardRotationMethods method is shouldAutomaticallyForwardRotationMethods . If YES (by default), then all children will receive the will and didRotateTo... methods, as well as the parent (and they, in turn, will send it to their children).

My solution (while there is more eloquent) is to request the last controller of the child view during the supportedInterfaceOrientations method and return its value. This allows me to rotate some areas while retaining only the portrait of others. I understand that it is fragile, but I see no other way that is not related to complicating events with calls, callbacks, etc.

0
Sep 24 '12 at 5:00
source share

If you are using a UINavigationController , you must implement shouldAutorotate and supportedInterfaceOrientations in a subclass of UINavigationController .

They can control two steps, if shouldAutorotate returns YES, then supportedInterfaceOrientations is valid. This is a very nice combination.

In this example, my main views are Portrait, except for CoverFlowView and PreviewView. Passing CoverFlowView to PreviewView, PreviewView wants to follow the rotation of CoverFlowCView.

 @implementation MyNavigationController -(BOOL)shouldAutorotate { if ([[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"PreviewView")]) return NO; else return YES; } -(NSUInteger)supportedInterfaceOrientations { if ([[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"CoverFlowView")]) return UIInterfaceOrientationMaskAllButUpsideDown; else return UIInterfaceOrientationMaskPortrait; } ... @end 
0
Sep 24 '12 at 15:58
source share

my solution: subclassed UINavigationController and set it as window.rootViewController

the top hierarchy view manager will control the orientation, some code examples: subclasses of UINavigationController

0
01 Oct
source share

The answers here pointed me in the right direction, although I could not get it to work just by cutting and pasting, because I use UINavigationControllers inside the UITabBarController. So my version in AppDelegate.m looks something like this that will work for UITabBarControllers, UINavigationControllers or UINavigationControllers in UITabBarController. If you use other custom containment controllers, you will need to add them here (which is kind of a chip).

 - (UIViewController*)terminalViewController:(UIViewController*)viewController { if ([viewController isKindOfClass:[UITabBarController class]]) { viewController = [(UITabBarController*)viewController selectedViewController]; viewController = [self terminalViewController:viewController]; } else if ([viewController isKindOfClass:[UINavigationController class]]) { viewController = [[(UINavigationController*)viewController viewControllers] lastObject]; } return viewController; } - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { NSUInteger orientations = UIInterfaceOrientationMaskPortrait; UIViewController* viewController = [self terminalViewController:window.rootViewController]; if (viewController) orientations = [viewController supportedInterfaceOrientations]; return orientations; } 

Another important thing: you must override supportedInterfaceOrientations in subclasses of the UIViewController, or by default what you specify in your Info.plist will be indicated.

0
02 Feb '13 at 3:36
source share



All Articles