Forum Xamarin Xamarin.iOS

Navigation controller from within a UITabBarController

MarthughieMarthughie GBMember ✭✭

An iOS App I am developing begins with a UINavigationController, and a navigation link from that displays a UITabBarController

The UITabBarController has three UIViewController within it, and for one I wish to be able to start another Navigation to another ViewController.

I had imagined the best way to do this would be to derive the View Controller for the tab in question from UINavigationController instead of from UIViewController.

However, the root Navigation Controller is initialised in AppDelegate, so presumably something similar is needed for the one in the TabBarController?

What initialisation is necessary for the second Navigation Controller, and, where should it be done ?



  • adamkempadamkemp USInsider, Developer Group Leader mod

    You almost never need to subclass either UINavigationController or UITabBarController. Instead, just create an instance of UINavigationController with an initial root view controller of your first view controller and then put the UINavigationController instance in the tab bar controller.

  • MarthughieMarthughie GBMember ✭✭

    Thank you @adamkemp. I will give that a go.

  • MarthughieMarthughie GBMember ✭✭

    Looking at the App Delegate code I am using for the NavController at the top of the App, the initial view controller is set like this

    initialViewController = Storyboard.InstantiateInitialViewController () as UINavigationController;
    m_window = new UIWindow (UIScreen.MainScreen.Bounds);
    m_window.RootViewController = initialViewController;

    Is this what I need to repeat? In other words should I be creating a new UIWindow based on the bounds of the screen and then setting that object's RootViewController to be the first view controller in the chain within the tab bar controller?

  • adamkempadamkemp USInsider, Developer Group Leader mod

    You only ever need to create one window so that code is not what you need.

    If you are using a storyboard for your tab controller then you can do the same thing you did for your root view controller of the whole application. That is, put a UINavigationController in your storyboard as the view controller for a given tab, and then drop your real view controller for that tab as the root of that navigation controller. It's just inserting a navigation controller in between.

    Personally I don't use storyboards (can't stand them), and so to do this in code you just do this:

    var myViewController = new MyViewController();
    var navigationController = new UINavigationController(myViewController);

    Then use navigationController wherever you previously used myViewController.

  • MarthughieMarthughie GBMember ✭✭

    Thank you @adamkemp. Understood.

  • MarthughieMarthughie GBMember ✭✭

    OK. I now see my problem a little better. Since I navigate from the very first UINavigationController in the App to the UITabController, I am already showing a NavBar in my tab controller (it shows a link back to the first UINavigationController).

    If I want to tag on navigation from this point for one of my tabs using this existing navigation controller what is the best way of doing it. Howe do I access the object (It was added in the storyboard).

    Also, since only one of the tabs needs to navigate I would need to update the navigation bar every time I changed tab, since one tab has a different navigation route to the others.

    Alternatively is there any guideline or other precedent that suggests I should leave the Nav Controller alone as a global navigation from the tab controller which can only go backwards, regardless of which tab is selected, and add another link some other way for the tab which needs further navigation.


  • adamkempadamkemp USInsider, Developer Group Leader mod

    Since I navigate from the very first UINavigationController in the App to the UITabController, I am already showing a NavBar in my tab controller (it shows a link back to the first UINavigationController).

    Tab controllers should not be nested in navigation controllers. Always go the other way around (navigation controllers nested in tab controllers). It's ok to present a tab controller modally, but it shouldn't be pushed inside a navigation controller.

    As an example, look at the iOS Music app. There's a tab bar at the bottom of the screen. That tab bar is almost always there, and the only exception is when something is pushed modally on top of it. You can navigate around within a tab, but each tab keeps track of its own navigation stack (i.e., each one has its own UINavigationController).

  • MarthughieMarthughie GBMember ✭✭

    Thank you @adamkemp, this is a very useful comment.

  • praveenaHMpraveenaHM USMember ✭✭

    Hello Team,
    I am working on xamarin.ios application using visual studio. Right now i am stuck to make connection between
    Naviagation Controller to View Controller in StoryBoard. how to achieve this in designer surface of storyboard.
    Please suggest me.

Sign In or Register to comment.