2017-03-03 43 views
0

我想实现菜单到我的应用程序。我有TabBarViewController -> NavigationViewController -> ContentViewController。当应用程序启动时,我打开HostViewController。而且在HostViewController里我必须使用这段代码:在TabBarController里面的ViewController

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let tabbarVC = storyboard.instantiateViewController(withIdentifier: "TabBarViewController") as! TabBarViewController 
contentList.append(tabbarVC as MenuItemContentViewController) 

但是MenuItemContentViewController只能继承UIViewController。所以我不能在tabbar中使用这段代码。

我试图

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let contentVC = storyboard.instantiateViewController(withIdentifier: "ContentViewController") as! ContentViewController 
contentList.append(contentVC as MenuItemContentViewController) 

但它增加了contentViewController没有的TabBar。

是否有人知道如何添加contentViewController tabbar和navbar?

谢谢。

+0

https://code.tutsplus.com/tutorials/ios-from-scratch-与-迅速,探索标签栏控制器 - CMS-25470 –

回答

0

尝试从UITabBarController 得到UIViewController实例使用

tabbarVC.viewControllers[index] as! MenuItemContentViewController 
1

按我的理解,你想contentViewController嵌入在导航栏的TabBar。您需要在任何情况下推动或addSubview到hostViewController即 “IBAction为或点击链接”

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let tabbarVC = storyboard.instantiateViewController(withIdentifier: "TabBarViewController") as! TabBarViewController 
self.present(tabbarVC, animated: True){} //In HostViewController's Self 

或者

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let tabbarVC = storyboard.instantiateViewController(withIdentifier: "TabBarViewController") as! TabBarViewController 
self.view.addSubview(tabbarVC.view) //In HostViewController's Self 
相关问题