2017-04-17 69 views
1

這裏我是動態添加標籤項目,但它只顯示4個項目(更多...標籤項目丟失)。我經歷了很多文章和stackoverflow的帖子,但沒有找到任何合適的解決方案。這是到目前爲止我的代碼...從視圖控制器動態加載tabItems時,TabBar沒有顯示[More ...]圖標

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    loadDynamicItems() 
} 

func loadDynamicItems() { 

    let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main) 
    var controllerArray = [UIViewController]() 

    for i in 1...8 { 
     let controller = storyBoard.instantiateViewController(withIdentifier: "newsCollection") as! NewsVC 
     let tabIcon = UIImage.fontAwesomeIcon(name: .addressBook, 
               textColor: UIColor.black, 
               size: CGSize(width: 30, height: 30)) 

     controller.tabBarItem = UITabBarItem(title: "item \(i)", image: tabIcon, tag: i) 
     controllerArray.append(controller) 
    } 

    self.setViewControllers(controllerArray, animated: true) 
} 

但是結果好像這IMAGE
爲什麼它僅顯示4項!

它應該加載一個[More ...]按鈕作爲第五個孩子,從那裏我可以訪問其餘的項目。 但爲什麼它不在那裏!?

+0

我認爲你必須使用UITabController和NOT的UIViewController。看這個教程:http://geekyviney.blogspot.com.es/2015/02/programmatically-designing.html – Alvaro

+0

爲什麼你'for'循環,當你只需要5個標籤欄項目時循環8個項目? –

+0

哪裏調用這個方法? – KKRocks

回答

0

當使用故事板時,爲了添加一個TabBarController到你的應用程序中,你需要拖放一個UITabBarViewController而不是一個普通的UIViewController。否則,某些屬性不能正確導出(如TabBar - 請參閱我的屏幕截圖)。

只要確保你刪除了自動創建的TabBar項目!

UITabBarViewController from Storyboard

相關問題