UITabBarController in xamarin.ios without using StoryBoard

妖精的绣舞 提交于 2019-12-11 02:28:54

问题


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


来源:https://stackoverflow.com/questions/47175839/uitabbarcontroller-in-xamarin-ios-without-using-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!