Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a UIViewController to Portrait orientation in iOS 6

As the ShouldAutorotateToInterfaceOrientation is deprecated in iOS 6 and I used that to force a particular view to portrait only, what is the correct way to do this in iOS 6? This is only for one area of my app, all other views can rotate.

like image 383
Neal Avatar asked Sep 20 '12 20:09

Neal


People also ask

How do I change the orientation of Xcode?

You can press Command + Arrow Key to change the iOS simulator screen orientation.


2 Answers

If you want all of our navigation controllers to respect the top view controller you can use a category so you don't have to go through and change a bunch of class names.

@implementation UINavigationController (Rotation_IOS6)  -(BOOL)shouldAutorotate {     return [[self.viewControllers lastObject] shouldAutorotate]; }  -(NSUInteger)supportedInterfaceOrientations {     return [[self.viewControllers lastObject] supportedInterfaceOrientations]; }  - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {     return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; }  @end 

As a few of the comments point to, this is a quick fix to the problem. A better solution is subclass UINavigationController and put these methods there. A subclass also helps for supporting 6 and 7.

like image 132
Anthony Avatar answered Nov 05 '22 13:11

Anthony


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

I'm using iOS6 with a storyboard that includes a UINavigationController set as the initial view controller.

//AppDelegate.m - this method is not available pre-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 whatever orientations you want to support for each UIViewController

- (NSUInteger)supportedInterfaceOrientations{     return UIInterfaceOrientationMaskPortrait; } 
like image 26
Phil Avatar answered Nov 05 '22 13:11

Phil