在导航控制器和TabbarController

时间:2017-05-23 13:55:23

标签: swift uinavigationcontroller uitabbarcontroller presentviewcontroller

来自一个单独的IntroViewController,我试图呈现一个嵌入在UINavigationController中的UIViewController,它也是UITabbarController中的三个选项之一。

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

当我使用上面的代码显示我的视图控制器时,导航控制器未完成,缺少所有视图控制器中的条形项(参见图像)。

enter image description here

我已经使用了几个小时,并且可以只显示TabbarController,UINavigationController,但不能同时显示两者。 谢谢你的帮助。非常感谢!

更新:我应该澄清一下,当我尝试从另一个View Controller呈现它时,问题才存在。当我将它用作初始视图控制器时,一切都很好。

1 个答案:

答案 0 :(得分:0)

使用此代码在tabBar控制器类中初始化viewControllers。 我为每个viewController和tabBarButton使用不同的storyboard,但你也可以从一个故事板初始化它们只是在makeTabBarItemForViewController方法中初始化右视图控制器

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!
}