-->

UITabBarController in xamarin.ios without using St

2019-07-08 18:44发布

问题:

In continue of my question in this post, I want to post a complete question which will be a question lots of xamarin.ios developers.

My request is having TabBar in ALL UIViewControllers. So, as I know, there are two ways to realize it.

First :

appDelegate -> set RootViewController : TabController -> UVC1

in this case, I have NULL NavigationController and I'll have no navigationItem. and in

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

It makes error that 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;
    }
}

And the second way :

RootViewController -> navigationController -> TabController -> UVC1 -> new UVC2 -> no tab bar !!

Here, everything sounds good, but when I navigate to new UIViewController which is not present in Tabs, the TabBar will diappear !

And the code is :

_tabController = new TabController();
var navigationController = new UINavigationController(viewController);
_window.RootViewController = new UINavigationController(_tabController);

What can I do ? Any idea?

I don't use StoryBoard !

回答1:

By wrapping all your UIViewController with UINavigationController you can enable the behaviour you want, but make sure that you remove TabBarController since the NavigationBar will overlap the NavigationBar from your Views.

_window.RootViewController = _tabController;

And your views:

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