2016-07-01 217 views
1

我面臨一個問題,我有一個viewControllernavigationControllertabBarController。無論何時在代碼中,我嘗試添加一個右邊或左邊的barButtonItem,或者甚至爲navigationBar添加一個標題,沒有任何反應。是因爲標籤欄?或者什麼可能的問題是酒吧按鈕項目未被添加

viewDidLoad

let addButton = UIBarButtonItem(title: "Add", style: .Plain, target: self, action: #selector(ProfileVC.addNewService)) 
navigationItem.rightBarButtonItem = addButton 

視圖控制器看在故事板

enter image description here

+0

你是否試圖在導航欄中添加barbutton?如果是的話,添加按鈕時應該存在self.navigationbar。 –

回答

1

嘗試:

parentViewController?.navigationItem.rightBarButtonItem = addButton 

因爲你的TabBarController嵌入NavigationController這樣的:

的UINavigationController - >的UITabBarController - > YourViewController

+0

謝謝,它工作完美。接受的答案! –

0

怎麼在這裏試試這個

進口的UIKit

一流的ViewController:UIViewController的{

override func viewDidLoad() { 
    super.viewDidLoad() 

    let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target: self, action: "buttonMethod") 
    navigationItem.leftBarButtonItem = addButton 

    navigationController?.navigationBar.barTintColor = UIColor.greenColor() 

} 

func buttonMethod() { 
    print("Perform action") 
} 

}

使用自定義欄按鈕試試這個

類的ViewController:UIViewController的{

override func viewDidLoad() { 
    super.viewDidLoad() 

    let btnName = UIButton() 
    btnName.setImage(UIImage(named: "imagename"), forState: .Normal) 
    btnName.frame = CGRectMake(0, 0, 30, 30) 
    btnName.addTarget(self, action: Selector("action"), forControlEvents: .TouchUpInside) 

    //.... Set Right/Left Bar Button item 
    let rightBarButton = UIBarButtonItem() 
    rightBarButton.customView = btnName 
    self.navigationItem.rightBarButtonItem = rightBarButton 

} 

func action() { 
    print("Perform action") 
} 

}

0

你navigationcontroller的RootViewController的是TabbarController 所以你必須創建自定義的UITabBarController類,然後把你的源代碼到viewDidLoad中定製的UITabbarController的功能。

樹視圖 - 控制將是:NavigationController - > UITabbarController->視圖控制器

或者你應該在TabBarController每個視圖控制器設置navigationcontroller

樹視圖 - 控制將是:UITabbarController-> NavigationController->視圖控制器