【问题标题】:Unable to set title for custom bar button?无法为自定义栏按钮设置标题?
【发布时间】:2016-11-09 13:25:12
【问题描述】:

我在我的应用程序中添加了一个自定义栏按钮。我想设置它的标题,但我无法设置它的标题。我在 swift 3 中使用以下代码。请建议最佳解决方案。

let customButton=UIButton(frame: CGRect(x: 0, y: 0, width: 60, height:30))
customButton.backgroundColor=Constant.Color.customBlue
customButton.titleLabel?.text = "Add"
customButton.titleLabel?.textColor=UIColor.white
let barButton = UIBarButtonItem(customView:customButton)
barButton.title="Add"
barButton.tintColor=UIColor.white
self.navigationItem.rightBarButtonItem=barButton

【问题讨论】:

标签: ios iphone swift xcode


【解决方案1】:

您需要如下设置标题:

 customButton?.setTitle("Add", for:.normal)

【讨论】:

    【解决方案2】:

    你必须使用这个:

    [customButton setTitle:@"Add" forState:UIControlStateNormal];
    

    【讨论】:

      【解决方案3】:

      试试这个代码:

      注意:这是通过操作将 barButton 应用到您的 navigationBar 的更好方法。

       navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action:  #selector(addTapped))
      
       func addTapped() {
          //Do your stuff here. After Add button pressed
          print("Add Button Pressed")
       }
      

      如果您仍想使用您的代码。添加以下代码以将按钮的标题设置为 KKRocks 答案。

        customButton?.setTitle("Add", for:.normal)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-22
        • 1970-01-01
        • 1970-01-01
        • 2021-06-03
        • 1970-01-01
        • 2014-02-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多