Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 6 AutoRotate In UiNavigationController

I have UiNavigationController in my app. I want that only one screen will be able to rotate so i put in this class :

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}

-(BOOL)shouldAutorotate {
    return YES;
}

-(NSInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

but the problem that happen is in ecery screen the app the rotation happens. how i can disable it?

like image 995
YosiFZ Avatar asked Dec 20 '25 01:12

YosiFZ


2 Answers

For iOS 6, I am using the following code in my app, which allows you to specify rotation for each viewcontroller individually:

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 originially I believe go to the Ray Wenderlich "iOS 6 by Tutorials" book. Ray Wenderlich website

like image 139
user1459524 Avatar answered Dec 21 '25 14:12

user1459524


Use the below code in the class you want to autorotate only:

@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 stack of navigation controller) that is to be rotated.

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
like image 36
Bhupendra Avatar answered Dec 21 '25 14:12

Bhupendra