Ios 未添加栏按钮项
我面临一个问题,我有一个Ios 未添加栏按钮项,ios,swift,navigation,Ios,Swift,Navigation,我面临一个问题,我有一个viewController,它既有navigationController又有tabBarController。无论何时在代码中,我尝试添加一个右侧或左侧的barButtonItem,甚至向导航栏添加一个标题都不会发生任何事情。是因为标签栏吗?或者问题是什么 在viewDidLoad let addButton = UIBarButtonItem(title: "Add", style: .Plain, target: self, action: #selector(P
viewController
,它既有navigationController
又有tabBarController
。无论何时在代码中,我尝试添加一个右侧或左侧的barButtonItem
,甚至向导航栏添加一个标题
都不会发生任何事情。是因为标签栏吗?或者问题是什么
在viewDidLoad
let addButton = UIBarButtonItem(title: "Add", style: .Plain, target: self, action: #selector(ProfileVC.addNewService))
navigationItem.rightBarButtonItem = addButton
下面是视图控制器在情节提要中的外观
试试这个
import UIKit
class 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")
}
}
正在使用自定义栏按钮,请尝试此操作
class 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")
}
}
尝试:
由于您的TabBarController嵌入在NavigationController中,如下所示:
UINavigationController->UITabBarcontroller->YourViewController您的导航控制器的rootviewcontroller是TabbarController 所以,您必须创建自定义的UITabbarController类,然后将源代码推入自定义UITabbarController的viewDidload函数中 树视图控制器将为:NavigationController-->UITabbarController->viewcontroller 或者,您应该在TabBarController中为每个ViewController设置navigationcontroller
树视图控制器将是:UITabbarController->NavigationController->viewcontroller您是否尝试在导航栏中添加按钮?如果是,添加按钮时应显示self.navigationbar。
parentViewController?.navigationItem.rightBarButtonItem = addButton