【问题标题】:iOS Xamarin pushing a tabBar onto a navigation controlleriOS Xamarin 将 tabBar 推送到导航控制器上
【发布时间】:2020-03-29 14:49:23
【问题描述】:

我有一个登录屏幕,一旦用户登录,我希望标签位于底部并在顶部有一个导航栏。我可以得到一个或另一个,但不能两者兼得。有可能吗?

    partial void LoginButton_TouchUpInside(UIButton sender)
    {
            // push tab controller
            tabController = new MyTabBarController();
            navController = new UINavigationController();
            navController.PushViewController(tabController, true);
    }

应用委托:

public override bool FinishedLaunching(UIApplication application, 
NSDictionary 
launchOptions)
    {
        // Override point for customization after application launch.
        // If not required for your application you can safely delete 
       this method

        //set root view controller
        Window = new UIWindow(UIScreen.MainScreen.Bounds);

        //Create navigation controller and tab controller
        UINavigationController navctrl = new 
   UINavigationController(new 
        StartViewController());
        MyTabBarController tabController = new MyTabBarController();

        Window.RootViewController = navctrl;

        Window.MakeKeyAndVisible();

        return true;
    }

【问题讨论】:

  • 嗨,你解决了吗?如果没有,你可以看看我的回答。如果有帮助,感谢您提前标记或投票 *.^

标签: ios xamarin controller tabs navigation


【解决方案1】:

您需要先将UINavigationController设置为RootViewController,然后将TabBarController添加到UINavigationController,这样就可以了。如下:

Window = new UIWindow(UIScreen.MainScreen.Bounds);

// Set up the first View Controller
var vc1 = new UIViewController();
vc1.View.BackgroundColor = UIColor.Orange;
vc1.TabBarItem.Title = "Orange";

// Set up the second View Controller
var vc2 = new UIViewController();
vc2.View.BackgroundColor = UIColor.Purple;
vc2.TabBarItem.Title = "Purple";

// Set up the Tab Bar Controller to have two tabs
var tabBarController = new UITabBarController();
tabBarController.ViewControllers = new UIViewController[] {vc1,vc2};

var registerController = new UINavigationController(tabBarController);

Window.RootViewController = registerController;
Window.MakeKeyAndVisible();

顺便说一句,从iOS 13开始你最好在SceneDelegate.cs的方法(WillConnect)中编写代码来设置RootViewController。

public void WillConnect (UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions)
{
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see UIApplicationDelegate `GetConfiguration` instead).

    UIWindowScene windowScene = new UIWindowScene(session, connectionOptions);

    // Set up the first View Controller
    var vc1 = new UIViewController();
    vc1.View.BackgroundColor = UIColor.Orange;
    vc1.TabBarItem.Title = "Orange";

    // Set up the second View Controller
    var vc2 = new UIViewController();
    vc2.View.BackgroundColor = UIColor.Purple;
    vc2.TabBarItem.Title = "Purple";

    // Set up the Tab Bar Controller to have two tabs
    var tabBarController = new UITabBarController();
    tabBarController.ViewControllers = new UIViewController[] {vc1,vc2};

    var registerController = new UINavigationController(tabBarController);
    this.Window = new UIWindow(windowScene);
    this.Window.RootViewController = registerController;
    this.Window.MakeKeyAndVisible();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 2013-04-22
    • 1970-01-01
    • 2020-07-14
    相关资源
    最近更新 更多