在某些UIViewControllers中使用XAMARIN.IOS UITabBarController

3

我有一个应用程序(Xamarin.IOS),它以没有TabBar的UIViewController(连接视图)开头。但是当用户登录后,我希望将我创建的选项卡栏添加到其他视图中。反之亦然,当用户注销时,我希望显示没有TabBar的连接视图。

我知道当我想要显示TabBar时,在appDelegate中,我必须像这样初始化_window:

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

如果我想要一个没有TabBar的视图,这里是appDelegate:

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

使用这个 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/Home@2x.png");

            tab2 = new UINavigationController(new OViewController(1));
            tab2.Title = Texts.Categories;
            tab2.TabBarItem.Image = UIImage.FromFile("Icons/Tag@2x.png");

            tab3 = new UINavigationController(new SearchViewController());
            tab3.Title = Texts.Search;
            tab3.TabBarItem.Image = UIImage.FromFile("Icons/Search@2x.png");

            tab4 = new UINavigationController(new BookmarkViewController(1));
            tab4.Title = Texts.Bookmarks;
            tab4.TabBarItem.Image = UIImage.FromFile("Icons/Favorite@2x.png");


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

            this.TabBar.BackgroundColor = UIColor.White;

            ViewControllers = tabs;
        }
    }

但是我怎么从带有TabBar的视图切换到没有TabBar的视图,反之亦然?

我不使用StoryBoard,在Xamarin.iOS上编写代码。

1个回答

6

制表键 -> 不使用制表键

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

在此输入图片描述

无选项卡 -> 选项卡

首先将连接页面设置为RootViewController,然后在需要时进行更改。

代码:

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();
    }
}

在“连接页面”中更改它。
if(connected){
     AppDelegate app = UIApplication.SharedApplication.Delegate as AppDelegate;
     app.changeRootVC();
}

enter image description here


ColeXia,它非常适合隐藏!它的工作效果非常好!但是对于反向操作,也就是说,如何在没有TabBar的ViewController中导航到具有TabBar的另一个ViewController?更详细地说,连接VC没有TabBar,在appDelegate中,我们有一个带有UINavigationController实例而不是TabBar的RootViewController,当用户连接时,它将被导航到具有TabBar的主页。你有什么想法吗? - Alireza
我认为最好的方法是从AppDelegate中调用_tabController = new TabController(); _window.RootViewController = _tabController; _window.MakeKeyAndVisible(); - ColeX
但是我在连接页面不需要TabBar!另外,连接不出现在我的TabBar中! - Alireza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接