繁体   English   中英

将 UITabBarController 设置为 rootViewController

[英]Set UITabBarController as rootViewController

我有一个UITabBarController ,如果用户会话仍然处于活动状态,我想显示此屏幕而不是登录屏幕。

我的UITabBarController有 3 个ViewControllers ,问题是我在底部看不到TabBar链接,而且我无法导航。

在此处输入图片说明

如果没有以下代码,一切正常。 我的意思是登录后我可以导航。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        FirebaseApp.configure()

        if Auth.auth().currentUser != nil {
            window = UIWindow(frame: UIScreen.main.bounds)
            window?.makeKeyAndVisible()
            window?.rootViewController = HomeTabBarController()
        }

        return true
    }

我也试过下面的代码来设置rootViewController但它是同样的问题。

当我尝试将其他视图控制器之一设置为根(子项之一)时,标签栏根本不显示

var rootView: MyRootViewController = MyRootViewController()

if let window = self.window{
    window.rootViewController = rootView
}

我在这里做错了什么?

在此处输入图片说明

我遇到了同样的问题,我遇到了你的帖子,你的代码的问题是HomeTabBarController()正在创建一个全新的 TabBarController 所以要修复它,请尝试我使用的以下方法。

if Auth.auth().currentUser != nil {

            print("******************************User Present******************************")
            self.window = UIWindow(frame: UIScreen.main.bounds)
            let storyboard = UIStoryboard(name: "Main", bundle: nil)

            // create view controllers from storyboard
            // Interface Builder -> Identitiy Inspector -> Storyboard ID
            // Set up the Tab Bar Controller to have two tabs
            let tabBarController  = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
            // Make the Tab Bar Controller the root view controller
            window?.rootViewController = tabBarController
            window?.makeKeyAndVisible()
        }

编辑确保将标识符添加到您的 TabBarController

 // Interface Builder -> Identitiy Inspector -> Storyboard ID
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        //// this code changes the initial point of aap///////

        window = UIWindow(frame: UIScreen.main.bounds)
        let nav = UINavigationController()
        let myview = SettingTabbar()
        nav.viewControllers = [myview]
        window?.rootViewController = nav
        window?.makeKeyAndVisible()

        return true
    }
And Function SettingTabbar is:
func SettingTabbar()->(UITabBarController)
{
    //Setting TabBar
    let tabbar = UITabBarController()

    //Designing Tabbar Item Images
    let table = UITabBarItem(title: nil, image:UIImage(named: "002-list") , tag: 0)
    let collection = UITabBarItem(title: nil, image: UIImage(named: "001-collect"), tag: 1)
    let insert = UITabBarItem(title: nil, image: UIImage(named: "add"), tag: 2)

    //Getting TabBar ViewControllers
    let TableView = newViewController()
    let CollectionView = PersonCollectionViewController()
    let InsertRec = nextViewController()
    //Setting ViewControllers on TabBar Items
    TableView.tabBarItem = table
    CollectionView.tabBarItem = collection
    InsertRec.tabBarItem = insert
    let controllers = [TableView,CollectionView,InsertRec]
    tabbar.viewControllers = controllers
    tabbar.viewControllers = controllers.map{UINavigationController(rootViewController: $0)}
    //Setting Title
    tabbar.navigationItem.title = "Person Record"

    return tabbar

}

问题是这一行:

window?.rootViewController = HomeTabBarController()

那是错误的 HomeTabBarController。 这是一个全新的 HomeTabBarController,没有孩子。 您需要获取故事板中的 HomeTabBarController。

我终于找到了解决方案:正如@matt 建议的那样,我必须获取故事板中的 HomeTabBarController。

    window = UIWindow(frame: UIScreen.main.bounds)


   let storyboard = UIStoryboard.init(name: "Main", bundle: nil)

   // controller identifier sets up in storyboard utilities
   // panel (on the right), it called Storyboard ID
   let viewController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController

   self.window?.rootViewController = viewController
   self.window?.makeKeyAndVisible()

   window?.makeKeyAndVisible()
   window?.rootViewController = viewController

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM