Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewController in UINavigationController orientation change

So I have the following hierarchy:

UINavigationController --> RootViewController (UIViewController) --> UITableViewController --> DetailViewController (UIViewController)

I want to lock the orientation on RootViewController to Portrait only, but leave all orientations for the rest view controllers.

If I put this to subclassed UINavigationController:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

All view controllers are then locked to portrait.

My question is, is there a way to lock only RootViewController to Portrait, but leave all options for other view controllers?

like image 236
1337code Avatar asked Jan 18 '13 15:01

1337code


2 Answers

in singleton

-(void)setOrientationSupport:(BOOL)flag{

    flag_orientationSupport_ = flag;
}

-(BOOL)getOrientationSupport{

    return flag_orientationSupport_;
}

in appdelegate

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{

    if ([singleton getOrientationSupport])
        return UIInterfaceOrientationMaskAll;
    else
        return UIInterfaceOrientationMaskPortrait;
}

add the following code in viewwillappear

for the viewcontroller you want support orientation

[singleton setOrientationSupport:YES];

to those controller you want disable orientation

[singleton setOrientationSupport:NO];
like image 52
chings228 Avatar answered Nov 13 '22 22:11

chings228


check the link here for fixing autorotation in iOS 6 and set orientation support per view basis: http://www.disalvotech.com/blog/app-development/iphone/ios-6-rotation-solution/

Here is what you could do:

  1. Create a custom navigation controller that is a subclass of UINavigationController, in your .m file:

     - (BOOL)shouldAutorotate
     {
          return self.topViewController.shouldAutorotate;
     }
     - (NSUInteger)supportedInterfaceOrientations
     {
          return self.topViewController.supportedInterfaceOrientations;
     }
    
  2. In your AppDelegate.h,

      @interface AppDelegate : UIResponder <UIApplicationDelegate> {
    
          UINavigationController *navigationController;
          ViewController *viewController;
      }
    
      @property (strong, nonatomic) UIWindow *window;
      @property (strong, nonatomic) ViewController *viewController;
    

    and in AppDelegate.m,

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
            // set initial view
           self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
           viewController = [[ViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    
           navigationController = [[CustomNavigationController alloc]
                        initWithRootViewController:viewController]; // iOS 6 autorotation fix
           [navigationController setNavigationBarHidden:YES animated:NO];
    
            self.window = [[UIWindow alloc]
               initWithFrame:[[UIScreen mainScreen] bounds]];
    
            [self.window setRootViewController:navigationController]; // iOS 6 autorotation fix
            //[self.window addSubview:navigationController.view];
    
            [self.window makeKeyAndVisible];
    
    
             return YES;
      }
    
    
      - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  // iOS 6 autorotation fix
      {
              return UIInterfaceOrientationMaskAll;
    
      }
    
  3. in your rootViewController, for whatever the event push the second view controller, do this:

      - (IBAction)pushSecondViewController:(id)sender {
    
        // push second view controller
        SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
        [self.navigationController pushViewController:secondViewController animated:YES];
       }
    
  4. in your each view controller, add

       - (BOOL)shouldAutorotate{}
       - (NSUInteger)supportedInterfaceOrientations{}
    

    for iOS 6, you can set each view controller whatever the orientation support you want individually.

    for iOS 5 and below, you can set

       - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{}
    

All the credits goes to John DiSalvo who wrote the sample app in the link.

Hope this helps.

like image 32
Raymond Wang Avatar answered Nov 13 '22 21:11

Raymond Wang