Search code examples
ios5uitabbarcontrolleruitabbar

Unable To Add Second Tab Bar to Tab Controller


I have two tab bars in order to have effect like the one shown in Question of Sliding UITabBarItems in UITabBarController

Instead of an arrow, I am trying to use tab bar item at last index to show another tab bar, and hiding the current tab bar. I am adding second tab bar programmatically in viewDidLoad. Problem is that my second tab bar is not showing up on when last tab bar item is tapped. What I have done is:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tabControler.view.frame = self.view.frame;
    self.tabControler.delegate = self;
    self.secondTabBarSelected = NO;

    self.secondTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 431, 320, 49)];
    self.secondTabBar.hidden = YES;
    self.secondTabBar.delegate = self;
    self.secondTabBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [self.tabControler.view addSubview:secondTabBar];

    [self.view addSubview:tabControler.view];
    NSLog(@"children of tabcon: %@",[tabControler.view subviews]); //Here second tab bar added with correct frame
}

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (!self.secondTabBarSelected) {
        NSLog(@"first tab bar");
        if([[tabBarController viewControllers] indexOfObject:viewController] == 3)
        {
            self.firstTabBar.hidden = YES;
            self.secondTabBar.hidden = NO;

        }
    }
}

Solution

  • I found that my tab bar was not visible because I was allocating it explicitly while it was present in nib file. Instead I just set the frame. My corrected code is:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
        self.tabControler.view.frame = self.view.frame;
        self.tabControler.delegate = self;
        self.secondTabBarSelected = NO;
    
        self.secondTabBar.frame = self.firstTabBar.frame;
        self.secondTabBar.hidden = YES;
        self.secondTabBar.delegate = self;
        NSLog(@"viewdidload frame: %@",secondTabBar.frame);
    
        self.secondTabBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
        [self.tabControler.view addSubview:self.secondTabBar];
        NSLog(@"viewdidload frame: %@",secondTabBar.frame);
    
    
        [self.view addSubview:tabControler.view];
    }
    
    -(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
    {
    
            NSLog(@"first tab bar - controller");
            if([[tabBarController viewControllers] indexOfObject:viewController] == 3)
            {
                self.firstTabBar.hidden = YES;
                self.secondTabBar.hidden = NO;
                self.secondTabBarSelected  = YES;
            }
    
    }