我遇到问题,我的viewController
同时包含navigationController
和tabBarController
。每当在代码中我尝试添加向右或向左barButtonItem
或甚至为navigationBar
添加标题时都不会发生任何事情。是因为标签栏吗?或者问题可能是什么
viewDidLoad
中的
let addButton = UIBarButtonItem(title: "Add", style: .Plain, target: self, action: #selector(ProfileVC.addNewService))
navigationItem.rightBarButtonItem = addButton
这里是视图控制器在故事板中的外观
答案 0 :(得分:2)
尝试:
parentViewController?.navigationItem.rightBarButtonItem = addButton
因为您的TabBarController嵌入在NavigationController中,如下所示:
UINavigationController - > UITabBarcontroller - > YourViewController
答案 1 :(得分: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")
}
}
答案 2 :(得分:0)
您的navigationcontroller的rootviewcontroller是TabbarController 所以你必须创建自定义的UITabbarController类,然后将你的源代码推送到自定义UITabbarController的viewDidload函数。
树视图控制器将是: NavigationController - > UITabbarController->的ViewController
或者您应该为TabBarController中的每个ViewController设置navigationcontroller
树视图控制器将是: UITabbarController-> NavigationController-> ViewController