2017-03-03 48 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 
相關問題