当我在appdelegate中设置Tabbar时,如何设置viewcontroller的rootview?

问题描述:

我已经在AppDelegate中设置了UITabBarController,并且已经将根视图设置为标签栏对象。但我需要将根视图设置为另一个视图控制器,而不会丢失标签栏的功能。我怎样才能做到这一点?当我在appdelegate中设置Tabbar时,如何设置viewcontroller的rootview?

let vc1 = ConstituencyViewController() 
    //vc1.view.backgroundColor = UIColor.orange 
    vc1.tabBarItem.title = "Search" 
    vc1.tabBarItem.image = UIImage(named: "Search") 

    // Set up the second View Controller 
    let vc2 = ConstDetailViewController() 
    //vc2.view.backgroundColor = UIColor.purple 
    vc2.tabBarItem.title = "User" 
    vc2.tabBarItem.image = UIImage(named: "Street View") 

    // Set up the Tab Bar Controller to have two tabs 
    let tabBarController = UITabBarController() 

    tabBarController.viewControllers = [vc1, vc2] 

    // Make the Tab Bar Controller the root view controller 
    window?.rootViewController = tabBarController 
    window?.makeKeyAndVisible() 
+0

你是什么意思没有松动的标签栏的功能??? –

+0

@VishalSonawane我的意思是我在rootview中设置的viewcontroller会直接在tabbar中的viewcontroller。那么我将如何设置根视图。? –

+0

你会同时使用TabbarController和另一个VC吗? –

let appDelegate = UIApplication.shared.delegate as! AppDelegate 

    let vc1 = ConstDetailViewController() 
      //vc1.view.backgroundColor = UIColor.orange 
      vc1.tabBarItem.title = "Search" 
      vc1.tabBarItem.image = UIImage(named: "Search") 

    let vc2 = OptionsViewController() 

    vc2.tabBarItem.title = "Search" 
    vc2.tabBarItem.image = UIImage(named: "Street View") 

      // Set up the second View Controller 

      //vc2.view.backgroundColor = UIColor.purple 

      // Set up the Tab Bar Controller to have two tabs 
      let tabBarController = UITabBarController() 

      tabBarController.viewControllers = [vc1,vc2] 
    appDelegate.window?.rootViewController = tabBarController 
    appDelegate.window?.makeKeyAndVisible() 
+0

我在尝试解决方案时收到了以下错误消息:“应用程序窗口预计在应用程序启动结束时有一个根视图控制器” –

+0

我已更新我的答案,请检查它。 –

+0

我在评论部分使用了上述代码。我在按钮操作中使用了它。似乎工作。请检查 –