Search code examples
iosuiviewcontrollerxamarin.iosuinavigationcontrollertabbar

XAMARIN.IOS UITabBarController in some UIViewControllers


I have an application (Xamarin.IOS) which start with a UIViewController (Connection view) with no TabBar. But when user Logged, I'd like to add the tabbar that I've created to other views. And vis-versa, when user logged out, I'd like to display the connection view without TabBar.

I know that when I want to display the TabBar, in appDelegate, I have to initialize _window like this :

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

and if I want to have a view without TabBar, here is appDelegate:

viewController = new ConnectionViewController();
_window.RootViewController = new UINavigationController(viewController);
_window.MakeKeyAndVisible();

with this TabController :

public class TabController : UITabBarController
    {

        UIViewController tab1, tab2, tab3, tab4;

        public TabController()
        {
            tab1 = new UINavigationController(new ListViewController());
            tab1.Title = Texts.Home;
            tab1.TabBarItem.Image = UIImage.FromFile("Icons/[email protected]");

            tab2 = new UINavigationController(new OViewController(1));
            tab2.Title = Texts.Categories;
            tab2.TabBarItem.Image = UIImage.FromFile("Icons/[email protected]");

            tab3 = new UINavigationController(new SearchViewController());
            tab3.Title = Texts.Search;
            tab3.TabBarItem.Image = UIImage.FromFile("Icons/[email protected]");

            tab4 = new UINavigationController(new BookmarkViewController(1));
            tab4.Title = Texts.Bookmarks;
            tab4.TabBarItem.Image = UIImage.FromFile("Icons/[email protected]");


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

            this.TabBar.BackgroundColor = UIColor.White;

            ViewControllers = tabs;
        }
    }

But how can I move from a view with TabBar to a view without and vis-versa ?

I don't use StoryBoard and I code on Xamarin.iOS.


Solution

  • Tab -> No Tab

    1. When Push

      ViewController2 vc2 = new ViewController2();
      vc2.HidesBottomBarWhenPushed = true; //add this line
      this.NavigationController.PushViewController(vc2, true);
      
    2. When Present

      this.PresentViewController(new ViewController2(), true, null);
      

    enter image description here

    No Tab -> Tab

    Set Connection Page as RootViewController at first, and then change it when you want to.

    Code:

    public partial class AppDelegate : UIApplicationDelegate
    {
        UIWindow window;
    
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            window = new UIWindow (UIScreen.MainScreen.Bounds);
    
            window.RootViewController = new UINavigationController(new ViewController1());
            window.MakeKeyAndVisible();
            return true;
        }
    
        public void changeRootVC()
        {
            window.RootViewController = new TabController();
        }
    }
    

    And change it in Connection Page

    if(connected){
         AppDelegate app = UIApplication.SharedApplication.Delegate as AppDelegate;
         app.changeRootVC();
    }
    

    enter image description here