Priyanka
Priyanka

Reputation: 513

orientation issues while using tab bar controller

I am using tab bar controller in my iPhone app having 4 tabs. I have written code for both orientations.

The problem is when I switch my tabs from portrait to landscape view I'm having trouble.For eg. if I'm in tab1 landscape mode and now I switch my tab and move to tab2 landscape mode but i can see portrait setting of that particular view and not the landscape view.

The code so far on my launch view controller that contains the tab bar controller is as follows :

-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    if (self.interfaceOrientation==UIInterfaceOrientationLandscapeLeft)
    {
        [self rotatingLandscape];
    }
    else if (self.interfaceOrientation==UIInterfaceOrientationLandscapeRight)
    {
        [self rotatingLandscape];
    }
    if (self.interfaceOrientation==UIInterfaceOrientationPortrait)
    {
        [self RotatingPotrait];
    }
    else if (self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
    {
        [self RotatingPotrait];
    }
    [super viewWillAppear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation==UIInterfaceOrientationPortrait) 
    { 
        rotate=YES; 
        [self RotatingPotrait];
    }
    if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
    { 
        rotate=YES;
        [self RotatingPotrait];
    }
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) 
    {
        rotate=NO;
        [self rotatingLandscape]; 
    }
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
        rotate=NO;
        [self rotatingLandscape]; 
    }
    return YES;
}

-(void)rotatingLandscape
{
    int selIndex = [self.tabBarController selectedIndex];
    if (selIndex == 0)
    {
         [view1p rotatingLandscape];
    }
    else if (selIndex == 1)
    {
        [view2 rotatingLandscape];
    }
    else if (selIndex == 2)
    {
        [view3 rotatingLandscape];
    }
    else if (selIndex == 3)
    {
        [view4 rotatingLandscape];
    }
    self.tabBarController.view.frame=CGRectMake(0, 0, 480, 300);
}

-(void)RotatingPotrait
{
    int selIndex = [self.tabBarController selectedIndex];
    if (selIndex == 0)
    {
        [view1p RotatingPotrait];
    }
    else if (selIndex == 1)
    {
        [view2 RotatingPotrait];
    }
    else if (selIndex == 2)
    {
        [view3 RotatingPotrait];
    }
    else if (selIndex == 3)
    {
        [view4 RotatingPotrait];
    }
    self.tabBarController.view.frame=CGRectMake(0, 0, 320, 460);
   }

Upvotes: 1

Views: 820

Answers (1)

Irina Didkovskaya
Irina Didkovskaya

Reputation: 59

I have fixed problem for my project support iOS 6.0+ Steps:

1 create Subclass of UITabBarViewController and add

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

2 Subclass UINavigationController add

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

3 to AppDelegate add

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;

    if(self.window.rootViewController){
        UIViewController *presentedViewController = [[[[(UINavigationController *)self.window.rootViewController viewControllers] objectAtIndex:self.tabBarController.selectedIndex] viewControllers] lastObject];
        orientations = [presentedViewController supportedInterfaceOrientations];
    }

    return orientations;
}

4 to each UIViewController add self.tabBarCotroller.delegate = self;

and delegate method

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
    NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
    NSLog(@"Tab index = %u ", indexOfTab);

    UIViewController *mVC = [[UIViewController alloc] init];
    mVC.view.backgroundColor = [UIColor redColor];
    mVC.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:mVC animated:NO completion:nil];
    [self dismissViewControllerAnimated:NO completion:nil];
}

5 to each UIViewController add orientation what you need

- (BOOL)shouldAutorotate
{
    if (self.interfaceOrientation==UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation==UIInterfaceOrientationLandscapeRight)
    {
        return NO;

    } else {
        return YES;
    }
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape; //UIInterfaceOrientationMaskPortraite
}

for me it is work

I have created example

Upvotes: 1

Related Questions