简体   繁体   中英

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?

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

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 (ie the just previous class on the stack of navigation controller) that is to be rotated.

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

In your appDelegate add the 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 in viewcontroller.m which is you want to rotate

- (void)viewDidLoad
{
    [super viewDidLoad];
    [myAppDelegate shouldAutoRotate:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [myAppDelegate shouldAutoRotate:NO];
}

I have done this for one of the my projects( IOS 7).It works for me perfectly.

You can create a category to override the methods in 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 the rotation in different controllers then override the supportedInterfaceOrientations and shouldAutorote in respective viewcontrollers changing the return value as required.

-(NSUInteger)supportedInterfaceOrientations
{
    return self.topViewController.supportedInterfaceOrientations;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM