Swift: Navigation Bar disappears after programatically embedding tab bar controller

匿名 (未验证) 提交于 2019-12-03 01:41:02

问题:

I have just programatically embedded a tab bar controller into my app, however it seems to have forced my Storyboard embedded Navigation Bars to disappear, I assume this is because I have now set the rootviewController to be my tab bar.

I read the answer to this post as it seemed that the problem was similar, however doing so prompts me with the error Pushing a navigation controller is not supported

Below is my code written in AppDelegate, here I create my tab-view and a navigation controller to push to the root view controller:

    // Set up the tab and navigation bar controllers     var currController = window?.rootViewController      let chatSB = UIStoryboard(name: "Chat", bundle: nil)     let mainSB = UIStoryboard(name: "Main", bundle: nil)      let tabBarController     = UITabBarController()     var navigationController = UINavigationController(rootViewController: currController!)      let profileVC = mainSB.instantiateViewControllerWithIdentifier("profileVC")   as TimelineTableViewController     let chatVC    = chatSB.instantiateViewControllerWithIdentifier("chatInboxVC") as ChatInboxViewController      tabBarController.viewControllers = [profileVC, chatVC, navigationController]     window?.rootViewController = tabBarController

How would I go about fixing this issue?

回答1:

If your desired view controllers are embedded in UINavigationController instances you need to instantiate those rather than the desired view controllers directly. The storyboard will take care of instantiating the embedded view controllers.

So, if your two navigation controller scenes have "profileNavController" and "chatInboxNavController" as their identifiers, your code would be -

// Set up the tab and navigation bar controllers     var currController = window?.rootViewController      let chatSB = UIStoryboard(name: "Chat", bundle: nil)     let mainSB = UIStoryboard(name: "Main", bundle: nil)      let tabBarController     = UITabBarController()     var navigationController = UINavigationController(rootViewController: currController!)      let profileNavController = mainSB.instantiateViewControllerWithIdentifier("profileNavController")   as UINavigationController     let chatNavController    = chatSB.instantiateViewControllerWithIdentifier("chatInboxNavController") as UINavigationController      tabBarController.viewControllers = [profileNavController, chatNavController, navigationController]     window?.rootViewController = tabBarController


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!