Question

My App has a UITabBarController with five tabs. I needed to rotate orientations only for the fifth tab. I was able to get all five to rotate to landscape by subclassing the UITabBarController

@implementation TabBarControllerRotate

-(BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)interfaceOrientation {

//return (interfaceOrientation == UIInterfaceOrientationPortrait);
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight 
          || interfaceOrientation == UIInterfaceOrientationLandscapeLeft
          || interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

if(tbc == nil){
    //tbc = [[UITabBarController alloc] initWithNibName:nil bundle:nil];        
    tbc = [[TabBarControllerRotate alloc] initWithNibName:nil bundle:nil];  ////// new  //////

....

tbc.viewControllers = 
[NSArray arrayWithObjects:viewController1, viewController2, viewController3
 ,viewController4,viewController5, nil];

I need now to turn off the rotation for viewController1 - 4 ; I tried unsuccessfully to do this by adding the following code to the four *.m files for these viewControllers.

- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)interfaceOrientation {

return NO;
}

Please advise me on how to Get R Done. Thanks for reading, Mark

Was it helpful?

Solution

I assume there are other easier ways to do this, but I finally got an solution. The title of the fifth viewController in the TabBarController, the only one that needs to change orientation, is "MyNotes"

Inside of SubListViewController.m the TabBarControllerRotate object is created.

  tbc = [[TabBarControllerRotate alloc] initWithNibName:nil bundle:nil];
  tbc.viewControllers = [NSArray arrayWithObjects:viewController1
          , viewController2, viewController3, viewController4, viewController5, nil
  tbc.delegate = self;

TabBarController subclasses UITabBarController and includes a BOOL property name bMyNotes.

SubListViewController is a UITabBarControllerDelegate

Each time the view is changed in the TabBarController a message is sent, namely - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

Therefore SubListViewController.m contains this code:

   #pragma mark UITabBarControllerDelegate methods

   - (void)tabBarController:(UITabBarController *)tabBarController 
        didSelectViewController:(UIViewController *)viewController{
    BOOL b = (viewController.title == @"MyNotes");
    [tbc setBMyNotesTab:b];
    //NSLog(@"MyNotesTab == %d", [tbc bMyNotesTab]);

   }

Then TabBarControllerRotate.m contains:

    - (BOOL)shouldAutorotateToInterfaceOrientation:
          (UIInterfaceOrientation)interfaceOrientation {

        //NSLog(@"bMyNotesTab is: %d",self.bMyNotesTab);

        return ( (interfaceOrientation == UIInterfaceOrientationLandscapeRight 
            || interfaceOrientation == UIInterfaceOrientationLandscapeLeft
            || interfaceOrientation == UIInterfaceOrientationPortrait
                 ) && self.bMyNotesTab 
               );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top