Disable autorotation on iOS 7 for one submitted ViewController

I have a UIViewController that is presented modally (fullscreen), and I would like to disable autorotation in this view. I do not want to limit it to a landscape or a portrait, I just want him to stay in any orientation that he originally represented.

In iOS 6, it was enough to simply override the method:

 - (BOOL)shouldAutorotate { return NO; } 

And he did exactly what I wanted. However, this does not seem to affect iOS 7. The method is called, but the return value is apparently ignored by the OS - it automatically rotates no matter what.

The documentation does not mention changes to this method. How can I achieve the desired effect on iOS 7?

Edit: The view controller is displayed (did not click!) On the UINavigationViewController :

 [self.navigationController presentViewController:vc animated:YES completion:nil]; 

Decision:

Oddly enough, but this decision was not published in numerous existing issues on this topic. On iOS 7, it seems that the response of the UINavigationController gives shouldAutorotate - this is what the OS is acting on. We need to subclass UINavigationController to change its behavior.

When working with a normal navigation stack, it is really quite simple to use [self.topViewController shouldAutorotate] , but when there is a modal view, it is in self.presentedViewController , not self.topViewController . So the complete solution looks like this:

 - (BOOL)shouldAutorotate { UIViewController *vc; if (self.presentedViewController) vc = self.presentedViewController; else vc = [self topViewController]; return [vc shouldAutorotate]; } 
+8
ios objective-c cocoa-touch uiviewcontroller
source share
2 answers

So, I just tried the code and it worked, which makes me believe that you are presenting your UIViewController in the UINavigationController . For some reason, iOS 7 has changed the way it UINavigationController .

The simplest solution is to create a subclass of UINavigationController that overrides the shouldAutorotate method and returns a value from topViewController.

 @interface CustomNavigationController : UINavigationController @end @implementation CustomNavigationController - (BOOL)shouldAutorotate { return [[self topViewController] shouldAutorotate]; } @end 

So, instead, where viewController is your object returning NO for shouldAutorotate .

 UINavigaitonController *navController = [UINavigationController alloc] initWithRootViewController:viewController]; [self presentViewController:navController animated:YES completion:nil]; 

Instead you should use a CustomNavigationController

 CustomNavigationController *customNavController = [CustomNavigationController alloc] initWithRootViewController:viewController]; [self presentViewController:customNavController animated:YES completion:nil]; 
+5
source share
 #import <objc/message.h> -(void)viewDidAppear:(BOOL)animated{ objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait ); } 

see How to programmatically set device orientation in iOS7?

But take the case when you use this method because it is a private API and your application may be rejected by Apple. So it might be better to set the orientation from the project details → General → Deployment Information tab, select “Landscape only” and “Landscape”. This may be the best approach if all of your views require only one kind of orientation.

-one
source share

All Articles