Best Way to add a tabbarController in a UIviewController which have a navigation controller?

1.7k views Asked by At

Just wandering that we can't add the tab bar controller in a uiviewcontroller in between of the app. What is the best way to implement such kind of UI where you need tab bar in between of any controller within the app and rest of other app is without tab bar. I tried to add a tab bar controller in between of the app but the controller which are being added in the tab bar items just lost few functionality like : not able to add button on them and they are not showing the navigation bar title which are being added on the tab bar controller.

How we can sort this out. Best way to implement the tab bar controller within middle of the app.

3

There are 3 answers

0
B25Dec On BEST ANSWER

Yes i got the answer for this after creating lots of POC to get this done. How we can do it by programmatically.

There is no need to make any subclassing or tool bar using. Write a function in delegate class in which you create your tab bar and add a root view controller to navigation controller.

This is going to work like in below way.

When you launch the first view controller with a navigation controller you will do llke this
[self.window setRootViewController:navcontroller];

Once you made your view controller you can make a function in which you will create the tab bar and its view controller. and this will seems like below code.

-(void)createTabbar {

UITabBarItem *tabBarItem1 = [tabBarCntrl.tabBar.items objectAtIndex:0];
tabBarItem1.title = @"First";
UITabBarItem *tabBarItem2 = [tabBarCntrl.tabBar.items objectAtIndex:1];
tabBarItem2.title = @"Second";


SecondViewController *viewController3 = [[SecondViewController alloc] init] ;
UINavigationController *nc2;
nc2 = [[UINavigationController alloc] initWithRootViewController:viewController3];



tabBarCntrl = [[UITabBarController alloc]init];
tabBarCntrl.viewControllers = [NSArray arrayWithObjects: nc2,nil];
[navcontroller pushViewController:tabBarCntrl animated:YES];

}

Once you will call it from shared delegate object tab bar will be added and when ever you want to remove it. you just need to create a funtion where you can remove subview from navigation controller.

-(void)removeTabbar
{
[navcontroller popToRootViewControllerAnimated:YES];

}

Now its done your UI will behave normally and we have achieved what we want to do.

2
iMemon On

I have worked on similar kind of app i-e Kubuto. The flow of this app was like this:

ECSlidingViewConroller --> UINavigationController --> Custom Container UIViewController with TabBar --> UIViewController

So you can have a hierarchy something like this: UINavigationController --> Custom Container UIViewController with TabBar --> UIViewController

import UIKit

class CustomTabbarControllerViewController: UIViewController, UITabBarDelegate {

  var storyboardIDs:[String] = ["FavouritesController","MoreController"]
  var viewControllers:[UIViewController] = []
  var activeController:UIViewController? = nil

  @IBOutlet weak var childView: UIView!
  @IBOutlet weak var tabbar: UITabBar!

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    //We have created child to just use its from on different screen(s) i-e iPhone4/5/6/6+ or iPad without changing any code, So its hidden now
    self.childView.hidden = true

    for storyboardID in self.storyboardIDs {
      var controller = self.storyboard?.instantiateViewControllerWithIdentifier(storyboardID) as UIViewController
      viewControllers.append(controller)
    }

    self.tabbar.delegate = self
    var firstItem = self.tabbar.items?[0] as UITabBarItem!
    self.tabbar.selectedItem = firstItem
    self.tabBar(tabbar, didSelectItem: firstItem)
  }

  func displayContentController(contentController:UIViewController) {
    self.addChildViewController(contentController)
    contentController.view.frame = self.childView.frame
    self.view.addSubview(contentController.view)
    contentController .didMoveToParentViewController(self)
    self.activeController = contentController
  }

  func hideContentController(contentController:UIViewController) {
    contentController.willMoveToParentViewController(nil)
    contentController.view .removeFromSuperview()
    contentController.removeFromParentViewController()
  }

  func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
    if let tempActiveController = activeController {
      self.hideContentController(tempActiveController)
    }
    switch item.tag {
    case 0: //Favourites
      self.displayContentController(viewControllers[0])
    case 1: //More
      self.displayContentController(viewControllers[1])
    default:
      break;
    }
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
}

Download Sample Code: https://www.dropbox.com/sh/fk84lxg7ns1wp3p/AACUrYQl6jI7WQ_GTp9kWj_6a?dl=0

Good Luck :)

1
Uttam Kadam On

Please use the navigation controller and toolbar to get this working.

In xp_AppDelegate.h add

@property (strong, nonatomic) UINavigationController *appNavController;

in xp_AppDelegate.m

_appNavController = [[UINavigationController alloc]initWithRootViewController:yourrootviewcontroller];

Create the UIController category where to add the toolbar.

And in your view controller you can include the category to call the function which plot the toolbar on your navigation bar.

The good part of this Is. You have good controller over the number of tabs for each view controller.

Please let me know your understanding, else I will put the working dummy code.