2016-07-01 569 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->视图控制器