简体   繁体   中英

UITabBarController in xamarin.ios without using StoryBoard

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 !

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM