以编程方式将 NavigationController 添加到基于 TabBar 的应用程序
Posted
技术标签:
【中文标题】以编程方式将 NavigationController 添加到基于 TabBar 的应用程序【英文标题】:Adding NavigationController to TabBar based app programmatically 【发布时间】:2018-08-14 22:01:43 【问题描述】:我在基于 customTabBar 的应用中有 6 个 viewController。 TabbBar 以编程方式添加。 现在我想添加一个导航控制器,以推动细节视图控制器从导航栏获得好处,但保持下面的 TabBar 作为在整个应用程序中导航的主要方式。
使用此代码,我在屏幕上获得了第一个带有导航栏的 uiviewcontroller“FirstVC”,但我无法从测试按钮“推送”详细信息“FirstDetail1VC”视图控制器
在我的 AppDelegate 中:
window = UIWindow(frame: UIScreen.main.bounds)
let myTabController = CustomTabbarController()
window?.rootViewController = myTabController
window?.makeKeyAndVisible()
在 my1VC 中(给出消息“无法推送 UINavigationController”):
func showNextVc()
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let newViewController = storyboard.instantiateViewController(withIdentifier: "FirstDetail1VC") as? FirstDetail1VC
let navigator = MyMainNavController
navigator.pushViewController(newViewController, animated: true)
在 CustomTabbarController 中更新后:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let my1VC = storyboard.instantiateViewController(withIdentifier: "FirstVC")
let my2VC = storyboard.instantiateViewController(withIdentifier: "SecondVC")
let my3VC = storyboard.instantiateViewController(withIdentifier: "ThirdVC")
let my4VC = storyboard.instantiateViewController(withIdentifier: "FourthVC")
let my5VC = storyboard.instantiateViewController(withIdentifier: "FifthVC")
let my6VC = storyboard.instantiateViewController(withIdentifier: "SixthVC")
my2VC.tabBarItem = UITabBarItem(title: "second", image: nil, selectedImage: nil)
my3VC.tabBarItem = UITabBarItem(title: "third", image: nil, selectedImage: nil)
my4VC.tabBarItem = UITabBarItem(title: "fourth", image: nil, selectedImage: nil)
my5VC.tabBarItem = UITabBarItem(title: "fifth", image: nil, selectedImage: nil)
my6VC.tabBarItem = UITabBarItem(title: "sixth", image: nil, selectedImage: nil)
let controllers = [my1VC, my2VC, my3VC, my4VC,my5VC, my6VC]
// if I use this one, text titles appears on tab bar, but cannot use navigation controller
viewControllers = controllers
// if I use this I have the navigation controlelr working but
tabBarbuttons not showing text title
// self.viewControllers = controllers.map
// UINavigationController(rootViewController: $0)
//
【问题讨论】:
How to insert UINavigationController inside UITabBarController的可能重复 关于您的问题已经有很好的教程:例如medium.com/@ITZDERR/… 或ios-blog.com/tutorials/quick-tips/…。 【参考方案1】:在设置所有 ViewController 的数组时,将 UINavigationController 分配给每个 UIViewController。
//Add NavigationController to all VC
viewControllers = [start, my1VC, my2VC, my3VC, my4VC,my5VC, my6VC].map
UINavigationController(rootViewController: $0)
【讨论】:
这行得通!但是现在 tabBar 不再显示按钮标题,直到我单击它们一次 您还必须将图像设置为 tabBarItem 即使使用 my2VC.tabBarItem = UITabBarItem(title: "2", image: nil, tag: 1) 标题也不会出现,否则,如果我不映射数组,标题会从第一次出现应用启动 UITabBarItem(title: "", image: ic_home, selectedImage: ic_home_filled) 需要将选中和未选中的图片图标设置为tabBarItem以上是关于以编程方式将 NavigationController 添加到基于 TabBar 的应用程序的主要内容,如果未能解决你的问题,请参考以下文章
如何以编程方式将 Google Sheet 脚本发布/部署为 API 可执行文件?