How to limit view controller orientation in iOS7 in the navigation controller hierarchy

My application is UITabBarController -> UINavigationController -> UITableViewController -> UIViewController.

I want to do 2 things:

  • Prevent the rotation of my table, I want it to always stay in the portrait.

  • FORCE and let my UIViewcontroller rotate the landscape.

What i know:

  • I understand that the viewcontroller at the top of the hierarchy controls rotation. Will it be my UITabBarController? Or rather, its only viewcontroller that objectIndex would have: 0?

  • My project settings allow me to rotate portrait, LL and LR. Im thinking that this is the pattern that I need to execute in order to solve this, allows ALL of the top level to rotate and then control each vc individually, right?

This is what I have found so far in SO.

So, for my top hierarchy, I set the project parameters to allow rotation to Portrait, LL and LR.

and then in my tableview controller, which I don't want to rotate:

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

and finally in my uiviewcontroller that I want to rotate:

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

However, this does not work. I can rotate them in any direction. I also don't know how to make LL rotate when I get to my uivc, which is called from a modal segment from my tablevc.

Any help in understanding this mess would be greatly appreciated :)

+7
ios7 orientation uiinterfaceorientation
source share
3 answers

Ok, here it is. kinda complicated.

Project settings must allow P, LL & LR

Storyboard is a UINavController with a UITableViewController with a button click on the UIViewController panel.

All scenes in the storyboard should be defined as orientation in the simulated indicators. Just saying, because after a while I had everything with different settings after testing.

Must have a class for NavController , TableViewController and the specified UIVController . My application appeared as Single view, then I dragged UITVC and finally introduced UITVC to UINVC. Then I connected the UIVC to the UITVC toolbar item that I dragged through the push segment.

Configure window.rootvc applications on navVC , your top vc hierarchy (remember to set this identifier in the storyboards or, of course, crash):

  UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; UINavigationController *myNavC = (UINavigationController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"MainNav"]; self.window.rootViewController = myNavC; 

Tell the big UINVC boss that everyone can spin:

  - (BOOL)shouldAutorotate { return self.topViewController.shouldAutorotate; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return self.topViewController.supportedInterfaceOrientations; } 

Limit the value of tablevc so that it does not rotate:

  - (BOOL)shouldAutorotate { return NO; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return (UIInterfaceOrientationMaskPortrait); } 

Allow rotation of the last UIVC:

  - (BOOL)shouldAutorotate { return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return (UIInterfaceOrientationMaskAllButUpsideDown); } 
+6
source share

Simple, but it works very well. IOS 7.1 and 8

AppDelegate.h

 @property () BOOL restrictRotation; 

AppDelegate.m

 -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if(self.restrictRotation) return UIInterfaceOrientationMaskPortrait; else return UIInterfaceOrientationMaskAll; } 

ViewController

 -(void) restrictRotation:(BOOL) restriction { AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; appDelegate.restrictRotation = restriction; } 

viewDidLoad

 [self restrictRotation:YES]; or NO 
+12
source share

Performance

Do not confuse UIDeviceOrientation and interface orientation here - my solution

Appdelegate.h

 @property () UIInterfaceOrientationMask restrictRotation; 

AppDelegate.m

 -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { UIInterfaceOrientationMask restrictionOrientation = 0; if (_restrictRotation == 0) return UIInterfaceOrientationMaskAll; UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation]; switch (currentOrientation) { case UIDeviceOrientationPortrait: if (!(UIInterfaceOrientationMaskPortrait & _restrictRotation)) restrictionOrientation = UIInterfaceOrientationMaskAll; else restrictionOrientation = UIInterfaceOrientationMaskPortrait; break; case UIDeviceOrientationPortraitUpsideDown: if (!(UIInterfaceOrientationMaskPortraitUpsideDown & _restrictRotation)) restrictionOrientation = UIInterfaceOrientationMaskAll; else restrictionOrientation = UIInterfaceOrientationMaskPortrait; break; case UIDeviceOrientationLandscapeLeft: if (!(UIInterfaceOrientationMaskLandscapeLeft & _restrictRotation)) restrictionOrientation = UIInterfaceOrientationMaskAll; else restrictionOrientation = UIInterfaceOrientationMaskPortrait; break; case UIDeviceOrientationLandscapeRight: if (!(UIInterfaceOrientationMaskLandscapeRight & _restrictRotation)) restrictionOrientation = UIInterfaceOrientationMaskAll; else restrictionOrientation = UIInterfaceOrientationMaskPortrait; break; case UIDeviceOrientationUnknown: if (!(UIInterfaceOrientationUnknown & _restrictRotation)) restrictionOrientation = UIInterfaceOrientationMaskAll; else restrictionOrientation = UIInterfaceOrientationMaskPortrait; break; default: NSLog(@"Unknown orientation"); break; } return restrictionOrientation; 

}

In each vc add function

 -(void) restrictRotation:(UIInterfaceOrientationMask) restriction { AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; appDelegate.restrictRotation = restriction; 

}

viewDidAppear

 // // Set vc orientation // [self restrictRotation:UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight]; or what you want. [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"]; or what you want. 
0
source share

All Articles