【问题标题】:iOS rightBarButtonItem on UINavigationController in swiftswift中UINavigationController上的iOS rightBarButtonItem
【发布时间】:2015-09-08 16:44:36
【问题描述】:

我正在尝试将 rightBarButtonItem 放在 UINavigationViewController 堆栈的第二个视图控制器上。

我正在创建并设置要显示的视图控制器的viewDidLoad 中的按钮。我的实际代码如下所示:

override func viewDidLoad() {
    super.viewDidLoad()
    menu_button_ = UIBarButtonItem(image: UIImage(named: "menu"),
        style: UIBarButtonItemStyle.Plain ,
        target: self, action: "OnMenuClicked:")

    self.navigationController!.navigationItem.rightBarButtonItem = menu_button_
}

我错过了什么?按钮没有出现。

【问题讨论】:

    标签: ios swift uinavigationcontroller uibarbuttonitem uinavigationitem


    【解决方案1】:

    在 Swift 5 中

    //For righter button item
    let rightBtn = UIBarButtonItem(image: UIImage(named: "rightmenu"), style: .plain, target: self, action: #selector(onClickMethod))//Change your function name and image name here
    self.navigationItem.rightBarButtonItem = rightBtn
    //self.navigationItem.rightBarButtonItem = [rightBtn, anotherBtn] //If you want to add more buttons add like this
    
    //This is your function
    @objc func onClickMethod() {
        print("Left bar button item")
    }
    

    【讨论】:

      【解决方案2】:

      您应该将menu_button_ 设置为viewControllerrightBarButtonItem,而不是navigationController

      试试

      self.navigationItem.rightBarButtonItem = menu_button_
      

      而不是

      self.navigationController!.navigationItem.rightBarButtonItem = menu_button_
      

      【讨论】:

      • 知道为什么它在通过导航控制器时不起作用吗?
      • “代替”是指“代替”吗?我很困惑
      • @DaniSpringer 是的,谢谢。我编辑了。我的意思是使用顶部的,不要使用底部的。
      • 明白了。谢谢!
      【解决方案3】:

      创建 UINavigationItem 的扩展,例如 -

      extension UINavigationItem {
          func addSettingButtonOnRight(){
              let button = UIButton(type: .custom)
          button.setTitle("setting", for: .normal)
          button.titleLabel?.font = UIFont.systemFont(ofSize: 15.0)
          button.layer.cornerRadius = 5
          button.backgroundColor = .gray
          button.frame = CGRect(x: 0, y: 0, width: 100, height: 25)
          button.addTarget(self, action: #selector(gotSettingPage), for: UIControlEvents.touchUpInside)
          let barButton = UIBarButtonItem(customView: button)
      
              self.rightBarButtonItem = barButton
          }
      
          @objc func gotSettingPage(){
      
          }
      }
      

      然后从 viewDidLoad() 中调用它,比如 -

      self.navigationItem.addSettingButtonOnRight()
      

      【讨论】:

      • 您为此创建了一个扩展程序吗?我不会这样做,我宁愿创建一个utils实例类并将这个函数放在里面,我们通常为一个我们会调用很多次的函数创建一个扩展,我不这样你会多次调用你的函数跨度>
      【解决方案4】:

      尝试关注code.,它对我有用。

      let homeButton : UIBarButtonItem = UIBarButtonItem(title: "LeftButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: "")
      
      let logButton : UIBarButtonItem = UIBarButtonItem(title: "RigthButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: "")
      
      self.navigationItem.leftBarButtonItem = homeButton
      self.navigationItem.rightBarButtonItem = logButton
      

      如果您想解决custom image,请查看以下链接上的苹果指南。

      https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/BarIcons.html#//apple_ref/doc/uid/TP40006556-CH21-SW1

      【讨论】:

      • 感谢@abhishek 的提示。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 2016-10-16
      • 2020-11-02
      • 1970-01-01
      相关资源
      最近更新 更多