Search code examples
iosuitabbaruitabbaritem

how to hide previous uiview from uitabbarcontroller when switching to another tabbaritem


I have two tabbars upper and bottom, when I click on tabbaritem UIView show but when I click to another tabbar item the previous view still remain in controller. The code is

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{


    switch (item.tag) {
        case 0:
        {
            NSLog(@"home");
            homeViewController *homeVc= [[homeViewController alloc] initWithNibName:@"homeViewController" bundle:nil];
            [self.view insertSubview:homeVc.view belowSubview:tabBar];
            break;
        }
        case 1:
        {

             NSLog(@"compus");
    compusViewController *compusVc= [[compusViewController alloc] initWithNibName:@"compusViewController" bundle:nil];
        [self.view insertSubview:compusVc.view belowSubview:tabBar];
            break;

        } 

Solution

  • There is no direct methods to simple remove all subviews. You have to call removeFromSuperview for all subviews. Luckily, the code is concise. Use [self.containerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]. – Sikhapol Saijit