IOS 6 AutoRotate in UiNavigationController
I have a UiNavigationController in my application. I only want one screen to be able to rotate, so I injected this class:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
-(BOOL)shouldAutorotate {
return YES;
}
-(NSInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
but the problem that happens is the ecery screen the rotation is taking place on. how can i disable it?
source to share
For iOS 6, I use the following code in my application, which allows you to separately specify the rotation for each view control:
AppDelegate.m -
- (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;
}
ViewController.m -
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
Credits for the code, I believe I believe in Rein Wenderlich's "iOS 6 by Tutorials" book. Ray Wenderlich website
source to share
Use the below code in a class you only want to autorotate:
@interface UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
- (NSUInteger)supportedInterfaceOrientations;
- (BOOL)shouldAutorotate;
@end
@implementation UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = (UINavigationController *) self.selectedViewController;
if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
return YES;
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations
{
if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = (UINavigationController *) self.selectedViewController;
if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = (UINavigationController *) self.selectedViewController;
if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
return YES;
}
return NO;
}
@end
Use this code in the parent view controller of the class (i.e. the just previous class on the navigation controller stack) that needs to be rotated.
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
source to share
In appDelegate add this code
@property(nonatomic,assign)BOOL shouldRotate;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
shouldRotate=NO;
}
-(void)shouldAutoRotate:(BOOL)rotate
{
self.shouldRotate=rotate;
}
And in your rootview controller add this code
#import "AppDelegate.h"
#define myAppDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate]
- (NSUInteger)supportedInterfaceOrientations
{
if(![myAppDelegate shouldRotate])
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAllButUpsideDown;
}
After that add this code to the viewcontroller.m you want to rotate
- (void)viewDidLoad
{
[super viewDidLoad];
[myAppDelegate shouldAutoRotate:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
[myAppDelegate shouldAutoRotate:NO];
}
I did this for one of my projects (IOS 7). It works great for me.
source to share
You can create a category to override the methods in the navigationController to support all classes.
@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
If you want to restrict rotation in different controllers, then override the supported InterfaceOrientations and shouldAutorote in the respective viewers, changing the return value as needed.
source to share