Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 未添加栏按钮项_Ios_Swift_Navigation - Fatal编程技术网

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