gpt4 book ai didi

swift - 在导航 Controller 和 TabbarController 中呈现 View Controller

转载 作者:行者123 更新时间:2023-11-28 08:09:14 25 4
gpt4 key购买 nike

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

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

当我向我的 View Controller 显示上面的代码时,导航 Controller 不完整,缺少所有 View Controller 中的栏项(参见图片)。

enter image description here

我已经研究了几个小时,只能显示 TabbarController 或 UINavigationController,但不能同时显示两者。谢谢你的帮助。非常感谢!

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

最佳答案

使用此代码在 tabBar Controller 类中初始化您的 viewControllers。我为每个 viewController 和 tabBarButton 使用不同的 Storyboard,但你也可以从一个 Storyboard初始化它们,只需在 makeTabBarItemForViewController 方法中初始化右 View Controller

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

关于swift - 在导航 Controller 和 TabbarController 中呈现 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44136854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com