2

As I'm new in Xamarin.IOS, I'd like to ask a question. I've followed this example for adding UITabBarController in a Xamarin.IOS project.

When I initialized RootViewController by an instance of TabController, it works fine and I have all tabs. BUT my NavigationController set null ! it means that :

  1. NavigationItem will disappear
  2. navigating between viewControllers are not possible by this code :

    this.NavigationController.PushViewController(new ProfileViewController(), true);
    

because the NavigationController is null ! Here is my code in AppDelegate:

_tabController = new TabController();
_window.RootViewController = _tabController;

and my TabController :

public class TabController : UITabBarController
    {

        UIViewController tab1, tab2, tab3, tab4;

        public TabController()
        {
            tab1 = new HomeViewController();
            tab1.TabBarItem.Image = UIImage.FromFile("Icons/Home.png");

            tab2 = new TagCategoryViewController(null, null, 1, null);
            tab2.TabBarItem.Image = UIImage.FromFile("Icons/Tag.png");

            tab3 = new SearchViewController();
            tab3.TabBarItem.Image = UIImage.FromFile("Icons/Search.png");

            tab4 = new ProfileViewController();
            tab4.TabBarItem.Image = UIImage.FromFile("Icons/Home.png");

            var tabs = new UIViewController[] {
                tab1, tab2, tab3,tab4
            };

            ViewControllers = tabs;
        }
    }

In additional, I have lots of UIViewControllers and I do all of them programmatically and I dont use StoryBoard !

ColeX
  • 14,062
  • 5
  • 43
  • 240
Alireza
  • 107
  • 5

1 Answers1

2

By wrapping your TabController in a UINavigationController.

_tabController = new TabController();
_window.RootViewController = new UINavigationController(_tabController);

This way NavigationController property won't be null and navigation can be done.

Mark Verkiel
  • 1,229
  • 10
  • 22
  • a little question, when I Navigate to a UIViewController which is not in the list of TabController, the tab bar disappear ! Do you know why? – Alireza Nov 07 '17 at 15:45
  • `UINavigationController` works with Stack navigation. Every `ViewController` that is pushed onto the stack will be shown above the `TabController`. This means that the `TabController` will disappear when pushed to a new `ViewController` and shown when pressing the back button – Mark Verkiel Nov 07 '17 at 15:57
  • So let's imagine, I have ViewController1 with TabController and I want to navigate to ViewController2 (which is not present in TabController) by keeping TabController. How can I do it? is it with PushViewController ? – Alireza Nov 07 '17 at 17:20
  • Instead of updating the question it is better to ask a new question. – Mark Verkiel Nov 08 '17 at 08:41
  • Ok, here is my new question : https://stackoverflow.com/questions/47175839/uitabbarcontroller-in-xamarin-ios-without-using-storyboard – Alireza Nov 08 '17 at 09:23