简体   繁体   中英

Swift: Navigation Bar disappears after programatically embedding tab bar controller

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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