【问题标题】:Bar button item is not being added条形按钮项目未添加
【发布时间】:2016-11-03 16:08:52
【问题描述】:

我面临一个问题,我有一个viewController,它同时具有navigationControllertabBarController。每当在代码中我尝试添加一个右或左barButtonItem 或什至添加一个标题到navigationBar 什么都没有发生。是因为标签栏吗?或者可能是什么问题

viewDidLoad

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

这里是视图控制器在故事板中的样子

【问题讨论】:

  • 您是否要在导航栏中添加 barbutton?如果是,self.navigationbar 应该在添加按钮时出现。

标签: ios swift navigation


【解决方案1】:

试试:

parentViewController?.navigationItem.rightBarButtonItem = addButton

因为您的 TabBarController 像这样嵌入到 NavigationController 中:

UINavigationController -> UITabBarcontroller -> YourViewController

【讨论】:

    【解决方案2】:

    你的 navigationcontroller 的 rootviewcontroller 是 TabbarController 所以你必须创建自定义的 UITabbarController 类,然后将你的源代码推送到自定义的 UITabbarController 的 viewDidload 函数中。

    树视图控制器将是:NavigationController --> UITabbarController->ViewController

    或者你应该在 TabBarController 中为每个 ViewController 设置 navigationcontroller

    树视图控制器将是:UITabbarController->NavigationController->ViewController

    【讨论】:

      【解决方案3】:

      试试这个

      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")
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-30
        相关资源
        最近更新 更多