在Xamarin Studio中隐藏/删除子视图

时间:2013-07-28 08:45:40

标签: c# xamarin

我通过AddChildViewController方法向视图添加了一个UITabBarController。然后我导航到下一个视图,我的标签栏出现,嘿presto。现在,我现在处于一个新视图中,如何删除子视图的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

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你希望你的初始ViewController是一个TabViewController,当用户在其中一个ViewControllers中选择某个东西时,它会转换到一个没有底部TabView按钮的ViewController。

如果是这样,这就是你需要做的事情。

在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";
    }

   ...