1
votes

Coming from a separate IntroViewController, I'm trying to present a UIViewController that's embedded in a UINavigationController, which is also one of three option in a UITabbarController.

let tabVC = storyboard?.instantiateViewController(withIdentifier: "TabBarViewController")
self.present(UINavigationController(rootViewController: tabVC!), animated: true, completion: nil)

When I present my View Controller with the code above, the Navigation controller isn't complete, missing the bar items from all the View Controllers (see image).

enter image description here

I've been on this for a few hours, and can either only get the TabbarController to show, the UINavigationController, but not both. Thanks for any help. Much appreciated!

UPDATE: I should clarify, the problem only exists when I try to present it from another View Controller. When I use it as an initial View Controller, everything is fine.

1
Check this maybe it would help. stackoverflow.com/questions/44122404/…Kegham K.

1 Answers

0
votes

Use this code to init your viewControllers in tabBar controller class. I use different storyboard for every viewController and tabBarButton but you also can init them from one storyboard just init right view controller in makeTabBarItemForViewController method

private func loadViewControllers(animated: Bool){

    var viewControllers = [UIViewController]()
    viewControllers.append(self.makeTabBarItemForViewController(storyboardIdentifier: "CurrentChannels", title: LocalizationUtil.localizedString(forKey: "On Air"), selectedImageName: Assets.tabBarIconOnAir.rawValue, imageName: Assets.tabBarIconOnAirUnselected.rawValue))
    self.viewControllers = viewControllers;
    self.setViewControllers(viewControllers, animated: animated)
}


private func makeTabBarItemForViewController(storyboardIdentifier: String,title:String , selectedImageName:String , imageName:String)-> UIViewController{
    let storyboard = UIStoryboard(name: storyboardIdentifier, bundle: nil)
    let vc:UIViewController? = storyboard.instantiateInitialViewController();
    let selectedImage = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
    let notSelectedImage = UIImage(named:selectedImageName)?.withRenderingMode(.alwaysOriginal)
    let item = UITabBarItem(title: title, image: selectedImage, selectedImage: notSelectedImage)
    vc!.tabBarItem = item

    if let navVC = vc as? UINavigationController{
        navVC.delegate = self
    }
    return vc!
}