iOS 6 AutoRotate In UiNavigationController

前端 未结 5 638
挽巷
挽巷 2020-12-10 08:46

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

-(BOOL)shouldAutorotateToInterfaceOrient         


        
相关标签:
5条回答
  • 2020-12-10 09:26

    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;
    }
    
    0 讨论(0)
  • 2020-12-10 09:37

    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.

    0 讨论(0)
  • 2020-12-10 09:39

    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.

    0 讨论(0)
  • 2020-12-10 09:40
    -(NSUInteger)supportedInterfaceOrientations
    {
        return self.topViewController.supportedInterfaceOrientations;
    
    }
    
    0 讨论(0)
  • 2020-12-10 09:41

    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

    0 讨论(0)
提交回复
热议问题