Best way to add tabbarController to UIviewController with navigation controller?

Just wandering around that we can't add a tab bar controller to the uiviewcontroller between apps. What is the best way to implement an interface like this where you want a tab bar between any controller in the app and the rest of another app without a tab bar. I tried to add a tab bar controller between apps, but the controller that is being added to the tab bar items just lost a few functionality like: could not add a button on them and they do not display the title of the nav bar that is added to the tab bar controller.

How can we figure this out. Best way to implement a tab bar controller in the middle of your application.

+3


source to share


3 answers


Yes, I got the answer for this after creating a lot of POCs to do this. How can we do this programmatically.

No need to do any subclasses or toolbars. Write a function in the delegate class where you create the tab bar and add the root view controller to the navigation controller.

This will work like below.

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

Once you've created your view controller, you can create a function where you create your tab bar and your view controller. and it will be similar to the code below.

- (invalid) 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];

      

}

After you select it from the general tab bar, the delegate object will be added and when you want to remove it. you just need to create a funtion where you can remove the subview from the navigation controller.

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

      

}

Your UI should now behave normally and we have achieved what we want.

0


source


I was working on a similar application ie Kubuto . The flow of this application was as follows:

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

So you can have a hierarchy like this: UINavigationController -> Custom UIViewController container 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 :)

+4


source


Please use a navigation controller and toolbar to get this to work.

In xp_AppDelegate.h add

@property (strong, non-atomic) UINavigationController * appNavController;

in xp_AppDelegate.m

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

Create a UIController category where you want to add the toolbar.

And in your view controller, you can include a category to call a function that displays the toolbar in the navigation bar.

The good part is. You have a good tab controller for each view controller.

Please let me know your understanding, otherwise I will put a working dummy code.

0


source







All Articles