【问题标题】:Tab Bar does not show on View Controller inside Navigation Controller导航控制器内的视图控制器上不显示标签栏
【发布时间】:2017-07-20 19:54:34
【问题描述】:

当我以编程方式打开新的视图控制器时遇到了这个问题。

let controller = self.storyboard?.instantiateViewController(withIdentifier: "overViewScreen") as! OverviewViewController
controller.user = self.userObject
let navigationController = UINavigationController(rootViewController: controller)
self.present(navigationController, animated: true, completion: nil)

我的项目结构: storyboard

在我的故事板上,标签栏显示在视图控制器上(右侧的表格),但是当我运行应用程序时,它看起来像这样: enter image description here

希望大家能帮帮我! 谢谢。

【问题讨论】:

    标签: ios swift uinavigationcontroller uitabbarcontroller uistoryboard


    【解决方案1】:

    您正在展示没有标签栏控制器的 NavigationController。您需要提供 TabBarController。

    提供您的 TabBarController 标识符并像使用控制器一样实例化它们

    评论代码:

    let tabVC = UIStoryboard(name: "NameOfYourStoryboard", bundle: Bundle.main).instantiateInitialViewController() as! UITabBarController
    
    let navVc = tabVC.viewControllers.first as! UINavigationController
    
    let vc = navVc.viewControllers.first as! OverviewViewController
    vc.incorrectAuthorization = SettingsAuthorizationMethod.fingerprint
    vc.user = self.userObject
    
    present(navController, animated: true, completion: nil)
    

    【讨论】:

    • 你的意思是这样的吗? let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "TabBarController") self.present(tabBarController!, animated: true, completion: nil)
    • 或类似的东西:让 tabVC = UIStoryboard(name: "NameOfYourStoryboard", bundle: Bundle.main).instantiateInitialViewController() as! UITabBarController 让 navVc = tabVC.viewControllers.first 为! UINavigationController 让 vc = navVc.viewControllers.first 为! LoginViewController vc.incorrectAuthorization = SettingsAuthorizationMethod.fingerprint vc.user = self.userObject present(navController, animated: true, completion: nil)
    • 所以现在我有:让 tabBarController = self.storyboard?.instantiateInitialViewController() 为! UITabBarController 让 navVc = tabBarController.childViewControllers.first 为! UINavigationController 让 vc = navVc.childViewControllers.first 为! OverviewViewController vc.user = self.userObject self.present(navVc, animated: true, completion: nil)。错误:无法将 LoginViewController 转换为 UITabBarController
    • LoginViewController 是我的班级名称,改成你的就行了。
    【解决方案2】:

    好的,我设法像这样修复它:

    let vc = self.storyboard?.instantiateViewController(withIdentifier: "TabBarController") as! TabBarController
                                vc.user = self.userObject
                                let nvc = UINavigationController(rootViewController: vc)
                                self.present(nvc, animated: true, completion: nil)
    

    我创建了一个单独的控制器类“TabBarController”,并为该类添加了一个属性“user”。在我的“OverViewController”中,我可以获得如下属性:

    let tabBar: TabBarController = tabBarController as! TabBarController
        user = tabBar.user
    

    感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 2010-10-09
      • 2012-06-23
      • 2014-10-06
      • 1970-01-01
      • 2015-12-25
      • 2017-05-14
      相关资源
      最近更新 更多