1

我通过 AddChildViewController 方法将 UITabBarController 添加到视图中。然后我导航到下一个视图,我的标签栏出现了,嘿。由于我现在在一个新视图中,如何删除子视图的 UITabBarController,因为当我使用 RemoveFromSuperview 函数时,它只会删除父视图,如果我推送另一个视图,该栏仍然会出现。

查看添加子视图的位置:

tabController = new UITabBarController ();

tabController.ViewControllers = new UIViewController[] {
    vc1,
    vc2,
    vc3
};
tabController.ViewControllers [0].TabBarItem.Title = "vc1";
tabController.ViewControllers [1].TabBarItem.Title = "vc2";
tabController.ViewControllers [2].TabBarItem.Title = "vc3";

this.NavigationController.AddChildViewController(tabController);
this.NavigationController.Add(tabController.View);

非常感激!

编辑 - 更多关于我的问题的详细信息:http ://forums.xamarin.com/discussion/6473/hide-tabbarcontroller-when-added-as-a-child-view#latest

4

1 回答 1

0

如果我理解正确,您希望您的初始 ViewController 成为 TabViewController,并且当用户在其中一个 ViewController 中选择某些内容时,它会转换为 ViewController 而底部没有 TabView 按钮。

如果是这样,这就是您需要做的。

在 AppDelegate.cs 中,您需要像这样设置窗口/视图控制器:

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
    window = new UIWindow (UIScreen.MainScreen.Bounds);
    rootController = new UINavigationController ();
    window.RootViewController = rootController;

    var tabController = new UITabBarController ();

    var vc1 = new TestController1();
    var vc2 = new TestController2();
    var vc3 = new TestController3();

    tabController.ViewControllers = new UIViewController[] {
        vc1,
        vc2,
        vc3
    };

    tabController.ViewControllers [0].TabBarItem.Title = "vc1";
    tabController.ViewControllers [1].TabBarItem.Title = "vc2";
    tabController.ViewControllers [2].TabBarItem.Title = "vc3";

    this.rootController.PushViewController(tabController, false);

    window.MakeKeyAndVisible ();

    return true;
}

完成此设置后,您可以从 TestController1 的类中转换到新视图,如下所示:

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();

    var button = UIButton.FromType(UIButtonType.RoundedRect);
    button.Frame = new RectangleF (10, 10, 200, 30);
    button.SetTitle ("Go Child Controller", UIControlState.Normal);

    button.TouchUpInside += (object sender, EventArgs e) => {
        NavigationController.PushViewController(new TestController4(), true);
    };

    View.AddSubview (button);
 }

此外,您可以简化设置选项卡按钮标题。

而不是这样做:

tabController.ViewControllers [0].TabBarItem.Title = "vc1";

你可以在你的子 UIViewController 中设置标题:

public class TestController1:UIViewController
{
    public TestController1 ()
    {
        this.Title = "vc1";
    }

   ...
于 2013-07-28T12:49:48.283 回答